Examples & One-Liners

Idiomatic Deft patterns for common tasks . Copy , paste , adapt .

String Operations

Uppercase a string
[upper "hello world"]
# => HELLO WORLD
Split and join
set words [split "one,two,three" ","]
[join $words " | "]
# => one | two | three
String interpolation
set name "Alice"
set greeting "Hello, $name!"
Pad and trim
[pad-left "42" 6 "0"]
# => "000042"

[trim "  hello  "]
# => "hello"
Replace and repeat
[replace "hello world" "world" "deft"]
# => "hello deft"

[repeat "ha" 3]
# => "hahaha"

Collections

List operations
set nums @{ 3 1 4 1 5 9 2 6 5 }
[sort $nums]    # => @{ 1 1 2 3 4 5 5 6 9 }
[reverse $nums] # => @{ 5 6 2 9 5 1 4 1 3 }
[unique $nums]  # => @{ 3 1 4 5 9 2 6 }
Filter and map
set nums @{ 1 2 3 4 5 6 7 8 9 10 }
[filter $nums { ($it > 5) }]
# => @{ 6 7 8 9 10 }

[map $nums { ($it * $it) }]
# => @{ 1 4 9 16 25 36 49 64 81 100 }
Reduce
set nums @{ 1 2 3 4 5 }
[reduce $nums 0 {| acc it | ($acc + $it) }]
# => 15
Map operations
set config %{ host "localhost" port 8080 debug true }
[keys $config]    # => @{ "host" "port" "debug" }
[values $config]  # => @{ "localhost" 8080 true }
$config~port      # => 8080
Set operations
set a ^{ 1 2 3 4 5 }
set b ^{ 3 4 5 6 7 }
[union $a $b]        # => ^{ 1 2 3 4 5 6 7 }
[intersection $a $b] # => ^{ 3 4 5 }
[difference $a $b]   # => ^{ 1 2 }

Control Flow

If / else
set x 42
if ($x > 10) {
    echo "big"
} else {
    echo "small"
}
Pattern matching
[match $value {
    0         => "zero"
    ^string   => "text: $value"
    @{ $h _ } => "head is $h"
    _         => "other"
}]
For loops
set users @{ %{ name "Alice" age 30 } %{ name "Bob" age 25 } }
for user $users {
    echo "$user~name is $user~age years old"
}
While loop
set i 0
while ($i < 5) {
    echo "tick $i"
    set i ($i + 1)
}

Functions & Types

Multi-clause functions
def area {|r^number|
    (3.14159 * $r * $r)
}
def area {w^number h^number} {
    ($w * $h)
}

[area 5]    # => 78.53975
[area 3 4]  # => 12
Types and protocols
deftype Point %{ x %{} y %{} }

defprotocol Showable {
    show {}
}

defimpl Point Showable {
    show {
        "($self~x, $self~y)"
    }
}

set p %Point{ x 10 y 20 }
[$p show]   # => (10, 20)
Pipelines
@{ 1 2 3 4 5 }
|> [map { ($it * 2) }]
|> [filter { ($it > 4) }]
|> [collect]
|> [join ", "]
# => "6, 8, 10"

HTTP & I/O

Simple HTTP handler
def index {|req res|
    [html 
        <h1>Hello!</h1>
        <p>Welcome to Deft</p>
    ]
}

def app %{
    routes @{
        %{ path "/" method :GET handler index }
    }
} |> [http/router]
Read files
# Read a file as string
set content [fs/read "config.txt"]

# List directory
set entries [fs/list "."]
HTML templates
def user-card {|user|
    [html 
        <div class="card">
            <h2>$user~name</h2>
            <p>$user~email</p>
        </div>
    ]
}
Static file serving
def app %{
    routes @{
        %{ path "/" method :GET handler index }
    }
    statics @{
        %{ path "/assets" dir "./public" }
    }
} |> [http/router]

Math

Basic math
(2 + 3) * 4       # => 20
[math/pow 2 10]         # => 1024
[math/sqrt 144]         # => 12
[abs -42]          # => 42
Aggregation
[math/sum 1 2 3 4 5]    # => 15
[math/avg 2 4 6 8]      # => 5
[min 3 1 4 1 5]    # => 1
[max 3 1 4 1 5]    # => 5
Rounding & clamping
[floor 3.8]        # => 3
[ceil 3.2]         # => 4
[round 3.5]        # => 4
[clamp 15 0 10]    # => 10