mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 17:41:41 +00:00
Improvements after Jens review
This commit is contained in:
parent
f6c3921900
commit
6d6d62b889
@ -47,8 +47,8 @@ It is a dialect of Lisp inspired by Phel and Janet.
|
|||||||
# Nesting forms works as you expect
|
# Nesting forms works as you expect
|
||||||
(+ 1 (- 3 2)) # = 1 + (3 - 2) => 2
|
(+ 1 (- 3 2)) # = 1 + (3 - 2) => 2
|
||||||
|
|
||||||
# Phel inherits PHP under the hood, so it can use native PHP (functions and classes) without any additional cost
|
# Phel inherits PHP under the hood, so it can use native PHP (functions and classes) without
|
||||||
# by using the `php/` prefix to all PHP native functions.
|
# any additional cost by using the `php/` prefix to all PHP native functions.
|
||||||
|
|
||||||
# Types
|
# Types
|
||||||
#############
|
#############
|
||||||
@ -100,16 +100,16 @@ string."
|
|||||||
# Collections & Sequences
|
# Collections & Sequences
|
||||||
#############
|
#############
|
||||||
|
|
||||||
# Lists are linked-list data structures, while vectors are array-backed.
|
# Lists are linked-list data structures, while vectors are array-backed
|
||||||
(type '(1 2 3)) # :list
|
(type '(1 2 3)) #=> :list
|
||||||
(type [1 2 3]) # :vector
|
(type [1 2 3]) #=> :vector
|
||||||
|
|
||||||
# A list would be written as just (1 2 3), but we have to quote
|
# 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.
|
# it to stop the reader thinking it's a function.
|
||||||
# Also, (list 1 2 3) is the same as '(1 2 3)
|
# Also, (list 1 2 3) is the same as '(1 2 3)
|
||||||
|
|
||||||
# You can produce a (non-lazy) sequence between a range.
|
# You can produce a (non-lazy) sequence between a range.
|
||||||
(range 1 10 2) # <- (range from to step)
|
(range 1 10 2) #=> (range from to step)
|
||||||
(take 4 (range 10))
|
(take 4 (range 10))
|
||||||
|
|
||||||
# Use cons to add an item to the beginning of a list
|
# Use cons to add an item to the beginning of a list
|
||||||
@ -132,35 +132,39 @@ string."
|
|||||||
#=> 10
|
#=> 10
|
||||||
|
|
||||||
(reduce push [] '(3 2 1))
|
(reduce push [] '(3 2 1))
|
||||||
# = (push (push (push [] 3) 2) 1)
|
#=> (push (push (push [] 3) 2) 1)
|
||||||
#=> [3 2 1]
|
#=> [3 2 1]
|
||||||
|
|
||||||
# Functions
|
# Functions
|
||||||
#############
|
#############
|
||||||
|
|
||||||
# Use fn to create new functions. A function always returns its last statement.
|
# Use fn to create new functions
|
||||||
(fn [] "Hello World") # => :function
|
# A function always returns its last statement
|
||||||
|
(fn [] "Hello World") #=> <function>
|
||||||
|
|
||||||
# You need extra parens to call it
|
# You need extra parens to call it
|
||||||
((fn [] "Hello World")) #=> "Hello World"
|
((fn [] "Hello World")) #=> "Hello World"
|
||||||
|
|
||||||
# You can create a var using def
|
# You can bind a value to a symbol using def for definition
|
||||||
(def x 1)
|
(def x 1)
|
||||||
x #=> 1
|
x #=> 1
|
||||||
|
|
||||||
# Assign a function to a var
|
# Variables provide a way to manage mutable state
|
||||||
|
(def foo (var 10)) # Define a variable with value 10
|
||||||
|
|
||||||
|
# Assign a function to a definition
|
||||||
(def hello-world (fn [] "Hello World"))
|
(def hello-world (fn [] "Hello World"))
|
||||||
(hello-world) #=> "Hello World"
|
(hello-world) #=> "Hello World"
|
||||||
|
|
||||||
# You can shorten this process by using defn
|
# You can shorten this process by using defn
|
||||||
(defn hello-world [] "Hello World")
|
(defn hello-world [] "Hello World")
|
||||||
|
|
||||||
# The [] is the list of arguments for the function.
|
# The [] is the list of arguments for the function
|
||||||
(defn hello [name]
|
(defn hello [name]
|
||||||
(str "Hello " name))
|
(str "Hello " name))
|
||||||
(hello "Jens") #=> "Hello Jens"
|
(hello "Jens") #=> "Hello Jens"
|
||||||
|
|
||||||
# You can also use this shorthand to create functions:
|
# You can also use this shorthand to create functions
|
||||||
(def hello2 |(str "Hello " $1))
|
(def hello2 |(str "Hello " $1))
|
||||||
(hello2 "Anna") #=> "Hello Anna"
|
(hello2 "Anna") #=> "Hello Anna"
|
||||||
|
|
||||||
@ -178,7 +182,7 @@ x # => 1
|
|||||||
# Maps
|
# Maps
|
||||||
#############
|
#############
|
||||||
|
|
||||||
# Hash maps have faster lookups but don't retain key order.
|
# Hash maps have faster lookups but don't retain key order
|
||||||
(type {:a 1 :b 2 :c 3}) #=> :hash-map
|
(type {:a 1 :b 2 :c 3}) #=> :hash-map
|
||||||
(type (hash-map :a 1 :b 2 :c 3)) #=> :hash-map
|
(type (hash-map :a 1 :b 2 :c 3)) #=> :hash-map
|
||||||
|
|
||||||
@ -199,7 +203,7 @@ keymap # => {:a 1 :c 3 :b 2}
|
|||||||
# Keywords can be used to retrieve their value from a map, too!
|
# Keywords can be used to retrieve their value from a map, too!
|
||||||
(:b keymap) #=> 2
|
(:b keymap) #=> 2
|
||||||
|
|
||||||
# Don't try this with strings.
|
# Don't try this with strings
|
||||||
# ("a" stringmap)
|
# ("a" stringmap)
|
||||||
# ...Exception: Call to undefined function a()
|
# ...Exception: Call to undefined function a()
|
||||||
|
|
||||||
@ -219,7 +223,7 @@ keymap # => {:a 1 :b 2 :c 3}
|
|||||||
# Sets
|
# Sets
|
||||||
#############
|
#############
|
||||||
|
|
||||||
# A Set contains unique values in random order.
|
# A Set contains unique values in random order
|
||||||
|
|
||||||
(type (set 1 2 3)) #=> :set
|
(type (set 1 2 3)) #=> :set
|
||||||
(set 1 2 3 1 2 3 3 2 1 3 2 1) #=> (set 1 2 3)
|
(set 1 2 3 1 2 3 3 2 1 3 2 1) #=> (set 1 2 3)
|
||||||
@ -230,17 +234,17 @@ keymap # => {:a 1 :b 2 :c 3}
|
|||||||
# Remove one with unset
|
# Remove one with unset
|
||||||
(unset (set 1 2 3) 1) #=> (set 2 3)
|
(unset (set 1 2 3) 1) #=> (set 2 3)
|
||||||
|
|
||||||
# Test for existence by using the set as a function:
|
# Test for existence by using the set as a function
|
||||||
((set 1 2 3) 1) #=> 1
|
((set 1 2 3) 1) #=> 1
|
||||||
((set 1 2 3) 4) #=> nil
|
((set 1 2 3) 4) #=> nil
|
||||||
|
|
||||||
# There are more functions like: count, union, intersection, difference, etc.
|
# There are more functions like: count, union, intersection, difference, etc
|
||||||
|
|
||||||
|
|
||||||
# Useful forms
|
# Useful forms
|
||||||
#############
|
#############
|
||||||
|
|
||||||
# Logic constructs in clojure are just macros, and look like everything else
|
# `If` conditionals in phel are special forms
|
||||||
(if false "a" "b") #=> "b"
|
(if false "a" "b") #=> "b"
|
||||||
(if false "a") #=> nil
|
(if false "a") #=> nil
|
||||||
|
|
||||||
@ -304,7 +308,7 @@ keymap # => {:a 1 :b 2 :c 3}
|
|||||||
# all native functions with the prefix `php/`.
|
# all native functions with the prefix `php/`.
|
||||||
(php/+ 1 2 3)
|
(php/+ 1 2 3)
|
||||||
|
|
||||||
# With :use you can use different namespaces. Similar as `use` in PHP.
|
# With :use you can use different namespaces. Similar as `use` in PHP
|
||||||
(ns my\module
|
(ns my\module
|
||||||
(:use \DateTimeImmutable))
|
(:use \DateTimeImmutable))
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user