2021-12-20 19:50:35 +03:00
|
|
|
---
|
|
|
|
language: phel
|
|
|
|
filename: learnphel.phel
|
|
|
|
contributors:
|
|
|
|
- ["Chemaclass", "https://github.com/Chemaclass"]
|
|
|
|
---
|
|
|
|
|
|
|
|
[Phel](https://phel-lang.org/) is a functional programming language that compiles to PHP.
|
2021-12-22 16:03:42 +03:00
|
|
|
It is a dialect of Lisp inspired by Clojure and Janet.
|
2021-12-20 19:50:35 +03:00
|
|
|
|
|
|
|
## Features
|
|
|
|
- Built on PHP's ecosystem
|
|
|
|
- Good error reporting
|
|
|
|
- Persistent Datastructures (Lists, Vectors, Maps and Sets)
|
|
|
|
- Macros
|
|
|
|
- Recursive functions
|
|
|
|
- Powerful but simple Syntax
|
|
|
|
- REPL
|
|
|
|
|
2022-01-21 19:15:19 +03:00
|
|
|
```newlisp
|
2021-12-20 19:50:35 +03:00
|
|
|
# Comments begin with a # character and continue until the end of the line. There are no multi-line comments.
|
|
|
|
|
|
|
|
# Phel is written in "forms", which are just
|
|
|
|
# lists of things inside parentheses, separated by whitespace.
|
|
|
|
|
|
|
|
# The first call in a file should be ns, to set the namespace
|
|
|
|
(ns learn-phel)
|
|
|
|
|
|
|
|
# More basic examples:
|
|
|
|
|
|
|
|
# str will create a string out of all its arguments
|
2021-12-21 01:48:28 +03:00
|
|
|
(str "Hello" " " "World") #=> "Hello World"
|
2021-12-20 19:50:35 +03:00
|
|
|
|
|
|
|
# Math is straightforward
|
2021-12-21 01:48:28 +03:00
|
|
|
(+ 1 1) #=> 2
|
|
|
|
(- 2 1) #=> 1
|
|
|
|
(* 1 2) #=> 2
|
|
|
|
(/ 2 1) #=> 2
|
2021-12-20 19:50:35 +03:00
|
|
|
|
|
|
|
# Equality is =
|
2021-12-21 01:48:28 +03:00
|
|
|
(= 1 1) #=> true
|
|
|
|
(= 2 1) #=> false
|
2021-12-20 19:50:35 +03:00
|
|
|
|
|
|
|
# You need not for logic, too
|
2021-12-21 01:48:28 +03:00
|
|
|
(not true) #=> false
|
2021-12-20 19:50:35 +03:00
|
|
|
|
|
|
|
# Nesting forms works as you expect
|
|
|
|
(+ 1 (- 3 2)) # = 1 + (3 - 2) => 2
|
|
|
|
|
2021-12-21 01:48:28 +03:00
|
|
|
# Phel inherits PHP under the hood, so it can use native PHP (functions and classes) without
|
|
|
|
# any additional cost by using the `php/` prefix to all PHP native functions.
|
2021-12-20 19:50:35 +03:00
|
|
|
|
|
|
|
# Types
|
|
|
|
#############
|
|
|
|
|
|
|
|
# Booleans are similar as the native PHP ones
|
|
|
|
|
|
|
|
nil
|
|
|
|
true
|
|
|
|
false
|
|
|
|
|
|
|
|
# Symbols are used to name functions and variables in Phel
|
|
|
|
# For example: symbol, snake_case_symbol, my-module/my-function
|
|
|
|
|
|
|
|
# Keywords are like symbols that begin with a colon character. However, they are used as constants rather than a name for something.
|
|
|
|
|
|
|
|
:keyword
|
|
|
|
:0x0x0x
|
|
|
|
::
|
|
|
|
|
|
|
|
# Numbers in Phel are equivalent to numbers in PHP
|
|
|
|
|
|
|
|
1337 # integer
|
|
|
|
+1337 # positive integer
|
|
|
|
-1337 # negative integer
|
|
|
|
|
|
|
|
1.234 # float
|
|
|
|
+1.234 # positive float
|
|
|
|
-1.234 # negative float
|
|
|
|
1.2e3 # float
|
|
|
|
7E-10 # float
|
|
|
|
|
|
|
|
# Strings are surrounded by double quotes. They almost work the same as PHP double quoted strings.
|
|
|
|
# A string can be written in multiple lines. The line break character is then ignored by the reader.
|
|
|
|
|
|
|
|
"hello world"
|
|
|
|
|
|
|
|
"this is\na\nstring"
|
|
|
|
|
|
|
|
"this
|
|
|
|
is
|
|
|
|
a
|
|
|
|
string."
|
|
|
|
|
|
|
|
"use backslack to escape \" string"
|
|
|
|
|
|
|
|
"the dollar must not be escaped: $ or $abc just works"
|
|
|
|
|
|
|
|
|
|
|
|
# Collections & Sequences
|
|
|
|
#############
|
|
|
|
|
2021-12-21 01:48:28 +03:00
|
|
|
# Lists are linked-list data structures, while vectors are array-backed
|
|
|
|
(type '(1 2 3)) #=> :list
|
|
|
|
(type [1 2 3]) #=> :vector
|
2021-12-20 19:50:35 +03:00
|
|
|
|
|
|
|
# A list would be written as just (1 2 3), but we have to quote
|
|
|
|
# it to stop the reader thinking it's a function.
|
|
|
|
# Also, (list 1 2 3) is the same as '(1 2 3)
|
|
|
|
|
|
|
|
# You can produce a (non-lazy) sequence between a range.
|
2021-12-21 01:48:28 +03:00
|
|
|
(range 1 10 2) #=> (range from to step)
|
2021-12-20 19:50:35 +03:00
|
|
|
(take 4 (range 10))
|
|
|
|
|
|
|
|
# Use cons to add an item to the beginning of a list
|
2021-12-21 01:48:28 +03:00
|
|
|
(cons 4 '(1 2 3)) #=> (4 1 2 3)
|
2021-12-20 19:50:35 +03:00
|
|
|
|
|
|
|
# Use push to add, and put to replace an item in a vector
|
2021-12-21 01:48:28 +03:00
|
|
|
(push [1 2 3] 4) #=> (1 2 3 4)
|
|
|
|
(put [1 2 3] 1 4) #=> (1 4 3)
|
2021-12-20 19:50:35 +03:00
|
|
|
|
|
|
|
# Use concat to add lists or vectors together
|
2021-12-21 01:48:28 +03:00
|
|
|
(concat [1 2] '(3 4)) #=> [1 2 3 4]
|
2021-12-20 19:50:35 +03:00
|
|
|
|
|
|
|
# Use filter, map to interact with collections
|
2021-12-21 01:48:28 +03:00
|
|
|
(map inc [1 2 3]) #=> [2 3 4]
|
|
|
|
(filter even? [1 2 3]) #=> [2]
|
2021-12-20 19:50:35 +03:00
|
|
|
|
|
|
|
# Use reduce to reduce them. The initial-value is mandatory
|
|
|
|
(reduce + 0 [1 2 3 4])
|
2021-12-21 01:48:28 +03:00
|
|
|
#=> (+ (+ (+ 1 2) 3) 4)
|
|
|
|
#=> 10
|
2021-12-20 19:50:35 +03:00
|
|
|
|
|
|
|
(reduce push [] '(3 2 1))
|
2021-12-21 01:48:28 +03:00
|
|
|
#=> (push (push (push [] 3) 2) 1)
|
|
|
|
#=> [3 2 1]
|
2021-12-20 19:50:35 +03:00
|
|
|
|
|
|
|
# Functions
|
|
|
|
#############
|
|
|
|
|
2021-12-21 01:48:28 +03:00
|
|
|
# Use fn to create new functions
|
|
|
|
# A function always returns its last statement
|
|
|
|
(fn [] "Hello World") #=> <function>
|
2021-12-20 19:50:35 +03:00
|
|
|
|
|
|
|
# You need extra parens to call it
|
2021-12-21 01:48:28 +03:00
|
|
|
((fn [] "Hello World")) #=> "Hello World"
|
2021-12-20 19:50:35 +03:00
|
|
|
|
2021-12-21 01:48:28 +03:00
|
|
|
# You can bind a value to a symbol using def for definition
|
2021-12-20 19:50:35 +03:00
|
|
|
(def x 1)
|
2021-12-21 01:48:28 +03:00
|
|
|
x #=> 1
|
2021-12-20 19:50:35 +03:00
|
|
|
|
2021-12-21 01:48:28 +03:00
|
|
|
# Variables provide a way to manage mutable state
|
|
|
|
(def foo (var 10)) # Define a variable with value 10
|
|
|
|
|
|
|
|
# Assign a function to a definition
|
2021-12-20 19:50:35 +03:00
|
|
|
(def hello-world (fn [] "Hello World"))
|
2021-12-21 01:48:28 +03:00
|
|
|
(hello-world) #=> "Hello World"
|
2021-12-20 19:50:35 +03:00
|
|
|
|
|
|
|
# You can shorten this process by using defn
|
|
|
|
(defn hello-world [] "Hello World")
|
|
|
|
|
2021-12-21 01:48:28 +03:00
|
|
|
# The [] is the list of arguments for the function
|
2021-12-20 19:50:35 +03:00
|
|
|
(defn hello [name]
|
|
|
|
(str "Hello " name))
|
2021-12-21 01:48:28 +03:00
|
|
|
(hello "Jens") #=> "Hello Jens"
|
2021-12-20 19:50:35 +03:00
|
|
|
|
2021-12-21 01:48:28 +03:00
|
|
|
# You can also use this shorthand to create functions
|
2021-12-20 19:50:35 +03:00
|
|
|
(def hello2 |(str "Hello " $1))
|
2021-12-21 01:48:28 +03:00
|
|
|
(hello2 "Anna") #=> "Hello Anna"
|
2021-12-20 19:50:35 +03:00
|
|
|
|
|
|
|
# Functions can pack extra arguments up in a seq for you
|
|
|
|
(defn count-args [& args]
|
|
|
|
(str "You passed " (count args) " args: " args))
|
2021-12-21 01:48:28 +03:00
|
|
|
(count-args 1 2 3) #=> "You passed 3 args: @[1 2 3]"
|
2021-12-20 19:50:35 +03:00
|
|
|
|
|
|
|
# You can mix regular and packed arguments
|
|
|
|
(defn hello-count [name & args]
|
|
|
|
(str "Hello " name ", you passed " (count args) " extra args"))
|
2021-12-21 01:48:28 +03:00
|
|
|
(hello-count "Jesus" 1 2) #=> "Hello Jesus, you passed 2 extra args"
|
2021-12-20 19:50:35 +03:00
|
|
|
|
|
|
|
|
|
|
|
# Maps
|
|
|
|
#############
|
|
|
|
|
2021-12-21 01:48:28 +03:00
|
|
|
# Hash maps have faster lookups but don't retain key order
|
|
|
|
(type {:a 1 :b 2 :c 3}) #=> :hash-map
|
|
|
|
(type (hash-map :a 1 :b 2 :c 3)) #=> :hash-map
|
2021-12-20 19:50:35 +03:00
|
|
|
|
|
|
|
# Maps can use any hashable type as a key, but usually keywords are best
|
|
|
|
# Keywords are like strings with some efficiency bonuses and they start with `:`
|
2021-12-21 01:48:28 +03:00
|
|
|
(type :a) #=> :keyword
|
2021-12-20 19:50:35 +03:00
|
|
|
|
|
|
|
(def stringmap {"a" 1 "b" 2 "c" 3})
|
2021-12-21 01:48:28 +03:00
|
|
|
stringmap #=> {"a" 1 "b" 2 "c" 3}
|
2021-12-20 19:50:35 +03:00
|
|
|
|
|
|
|
(def keymap {:a 1 :b 2 :c 3})
|
2021-12-21 01:48:28 +03:00
|
|
|
keymap #=> {:a 1 :c 3 :b 2}
|
2021-12-20 19:50:35 +03:00
|
|
|
|
|
|
|
# Retrieve a value from a map by calling it as a function
|
2021-12-21 01:48:28 +03:00
|
|
|
(stringmap "a") #=> 1
|
|
|
|
(keymap :a) #=> 1
|
2021-12-20 19:50:35 +03:00
|
|
|
|
|
|
|
# Keywords can be used to retrieve their value from a map, too!
|
2021-12-21 01:48:28 +03:00
|
|
|
(:b keymap) #=> 2
|
2021-12-20 19:50:35 +03:00
|
|
|
|
2021-12-21 01:48:28 +03:00
|
|
|
# Don't try this with strings
|
2021-12-20 19:50:35 +03:00
|
|
|
# ("a" stringmap)
|
|
|
|
# ...Exception: Call to undefined function a()
|
|
|
|
|
|
|
|
# Retrieving a non-present key returns nil
|
2021-12-21 01:48:28 +03:00
|
|
|
(stringmap "d") #=> nil
|
2021-12-20 19:50:35 +03:00
|
|
|
|
|
|
|
# Use put to add new keys to hash-maps
|
|
|
|
(def newkeymap (put keymap :d 4))
|
2021-12-21 01:48:28 +03:00
|
|
|
newkeymap #=> {:a 1 :b 2 :c 3 :d 4}
|
2021-12-20 19:50:35 +03:00
|
|
|
|
|
|
|
# But remember, phel types are immutable!
|
2021-12-21 01:48:28 +03:00
|
|
|
keymap #=> {:a 1 :b 2 :c 3}
|
2021-12-20 19:50:35 +03:00
|
|
|
|
|
|
|
# Use unset to remove keys
|
2021-12-21 01:48:28 +03:00
|
|
|
(unset keymap :a) #=> {:b 2 :c 3}
|
2021-12-20 19:50:35 +03:00
|
|
|
|
|
|
|
# Sets
|
|
|
|
#############
|
|
|
|
|
2021-12-21 01:48:28 +03:00
|
|
|
# A Set contains unique values in random order
|
2021-12-20 19:50:35 +03:00
|
|
|
|
2021-12-21 01:48:28 +03:00
|
|
|
(type (set 1 2 3)) #=> :set
|
|
|
|
(set 1 2 3 1 2 3 3 2 1 3 2 1) #=> (set 1 2 3)
|
2021-12-20 19:50:35 +03:00
|
|
|
|
|
|
|
# Add a member with push
|
2021-12-21 01:48:28 +03:00
|
|
|
(push (set 1 2 3) 4) #=> (set 1 2 3 4)
|
2021-12-20 19:50:35 +03:00
|
|
|
|
|
|
|
# Remove one with unset
|
2021-12-21 01:48:28 +03:00
|
|
|
(unset (set 1 2 3) 1) #=> (set 2 3)
|
2021-12-20 19:50:35 +03:00
|
|
|
|
2021-12-21 01:48:28 +03:00
|
|
|
# Test for existence by using the set as a function
|
|
|
|
((set 1 2 3) 1) #=> 1
|
|
|
|
((set 1 2 3) 4) #=> nil
|
2021-12-20 19:50:35 +03:00
|
|
|
|
2021-12-21 01:48:28 +03:00
|
|
|
# There are more functions like: count, union, intersection, difference, etc
|
2021-12-20 19:50:35 +03:00
|
|
|
|
|
|
|
|
|
|
|
# Useful forms
|
|
|
|
#############
|
|
|
|
|
2021-12-21 01:48:28 +03:00
|
|
|
# `If` conditionals in phel are special forms
|
|
|
|
(if false "a" "b") #=> "b"
|
|
|
|
(if false "a") #=> nil
|
2021-12-20 19:50:35 +03:00
|
|
|
|
|
|
|
# Use let to create temporary bindings
|
|
|
|
(let [a 1 b 2]
|
2021-12-21 01:48:28 +03:00
|
|
|
(> a b)) #=> false
|
2021-12-20 19:50:35 +03:00
|
|
|
|
|
|
|
# Group statements together with do
|
|
|
|
(do
|
|
|
|
(print "Hello")
|
2021-12-21 01:48:28 +03:00
|
|
|
"World") #=> "World" (prints "Hello")
|
2021-12-20 19:50:35 +03:00
|
|
|
|
|
|
|
# Functions have an implicit do
|
|
|
|
(defn print-and-say-hello [name]
|
|
|
|
(print "Saying hello to " name)
|
|
|
|
(str "Hello " name))
|
|
|
|
(print-and-say-hello "Jeff") #=> "Hello Jeff" (prints "Saying hello to Jeff")
|
|
|
|
|
|
|
|
# So does let
|
|
|
|
(let [name "Urkel"]
|
|
|
|
(print "Saying hello to " name)
|
2021-12-21 01:48:28 +03:00
|
|
|
(str "Hello " name)) #=> "Hello Urkel" (prints "Saying hello to Urkel")
|
2021-12-20 19:50:35 +03:00
|
|
|
|
|
|
|
# Use the threading macros (-> and ->>) to express transformations of
|
|
|
|
# data more clearly.
|
|
|
|
|
|
|
|
# The "Thread-first" macro (->) inserts into each form the result of
|
|
|
|
# the previous, as the first argument (second item)
|
|
|
|
(->
|
2021-12-21 01:48:28 +03:00
|
|
|
{:a 1 :b 2}
|
2021-12-20 19:50:35 +03:00
|
|
|
(put :c 3) #=> (put {:a 1 :b 2} :c 3)
|
|
|
|
(unset :b)) #=> (unset (put {:a 1 :b 2} :c 3) :b)
|
|
|
|
|
|
|
|
|
|
|
|
# The double arrow does the same thing, but inserts the result of
|
|
|
|
# each line at the *end* of the form. This is useful for collection
|
|
|
|
# operations in particular:
|
|
|
|
(->>
|
|
|
|
(range 10)
|
|
|
|
(map inc) #=> (map inc (range 10))
|
|
|
|
(filter odd?)) #=> (filter odd? (map inc (range 10)))
|
|
|
|
# Result: [1 3 5 7 9]
|
|
|
|
|
|
|
|
|
|
|
|
# When you are in a situation where you want more freedom as where to
|
2021-12-21 01:48:28 +03:00
|
|
|
# put the result of previous data transformations in an
|
2021-12-20 19:50:35 +03:00
|
|
|
# expression, you can use the as-> macro. With it, you can assign a
|
|
|
|
# specific name to transformations' output and use it as a
|
|
|
|
# placeholder in your chained expressions:
|
|
|
|
|
|
|
|
(as-> [1 2 3] input
|
|
|
|
(map inc input) #=> You can use last transform's output at the last position
|
|
|
|
(get input 2) #=> and at the second position, in the same expression
|
|
|
|
(push [4 5 6] input 8 9 10)) #=> or in the middle !
|
|
|
|
# Result: [4 5 6 4 8 9 10]
|
|
|
|
|
|
|
|
# PHP
|
|
|
|
#################
|
|
|
|
|
2021-12-21 01:48:28 +03:00
|
|
|
# PHP has a huge and useful standard library, and you're able to use
|
2021-12-20 19:50:35 +03:00
|
|
|
# all native functions with the prefix `php/`.
|
|
|
|
(php/+ 1 2 3)
|
|
|
|
|
2021-12-21 01:48:28 +03:00
|
|
|
# With :use you can use different namespaces. Similar as `use` in PHP
|
2021-12-20 19:50:35 +03:00
|
|
|
(ns my\module
|
|
|
|
(:use \DateTimeImmutable))
|
|
|
|
|
|
|
|
# You can import functions from other phel files with :require
|
|
|
|
(ns my\module
|
|
|
|
(:require phel\test :refer [deftest is]))
|
|
|
|
|
|
|
|
# Use the class name with a "php/new" to make a new instance
|
|
|
|
(php/new \DateTime) # <a date-time object>
|
|
|
|
|
|
|
|
# Use php/-> to call methods of an object
|
|
|
|
(def d (php/new \DateTime))
|
|
|
|
(php/-> d (getTimestamp)) # <a timestamp>
|
|
|
|
|
|
|
|
# you can do it in one line too
|
|
|
|
(php/-> (php/new \DateTime) (getTimestamp))
|
|
|
|
|
|
|
|
# Use php/:: to call static methods
|
|
|
|
(php/:: \DateTimeImmutable ATOM) # <a timestamp>
|
|
|
|
```
|
|
|
|
|
|
|
|
### Further Reading
|
|
|
|
|
|
|
|
This is far from exhaustive, but hopefully it's enough to get you on your feet.
|
|
|
|
|
|
|
|
Read the full documentation in the website: [https://phel-lang.org/](https://phel-lang.org/documentation/getting-started/)
|