mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-08-07 07:16:42 +02:00
[Janet/en] Update janet.html.markdown (#4283)
* Update janet.html.markdown Minor edits. * Update janet.html.markdown * Update janet.html.markdown A few more improvements.
This commit is contained in:
@@ -58,7 +58,8 @@ math/e # => 2.71828
|
|||||||
"hello"
|
"hello"
|
||||||
"hey\tthere" # contains a tab
|
"hey\tthere" # contains a tab
|
||||||
|
|
||||||
# For multi-line strings, use one or more backticks. No escapes allowed.
|
# For multi-line strings, use one or more backticks. Backslash-escapes not
|
||||||
|
# recognized in these (bytes will be parsed literally).
|
||||||
``a long
|
``a long
|
||||||
multi-line
|
multi-line
|
||||||
string`` # => "a long\nmulti-line\nstring"
|
string`` # => "a long\nmulti-line\nstring"
|
||||||
@@ -81,7 +82,8 @@ one here`
|
|||||||
|
|
||||||
# See the string library for more (splitting, replacement, etc.)
|
# See the string library for more (splitting, replacement, etc.)
|
||||||
|
|
||||||
# Arrays and Tuples ###########################################################
|
# Data Structures #############################################################
|
||||||
|
# Arrays and Tuples
|
||||||
# Arrays are mutable, tuples are immutable.
|
# Arrays are mutable, tuples are immutable.
|
||||||
|
|
||||||
# Arrays (mutable)
|
# Arrays (mutable)
|
||||||
@@ -91,18 +93,19 @@ one here`
|
|||||||
# Tuples (immutable)
|
# Tuples (immutable)
|
||||||
# Note that an open paren usually indicates a function call, so if you want a
|
# Note that an open paren usually indicates a function call, so if you want a
|
||||||
# literal tuple with parens, you need to "quote" it (with a starting single
|
# literal tuple with parens, you need to "quote" it (with a starting single
|
||||||
# quote mark).
|
# quote mark)...
|
||||||
'(4 5 6)
|
'(4 5 6)
|
||||||
[4 5 6] # ... or just use square brackets.
|
[4 5 6] # ... or just use square brackets.
|
||||||
|
|
||||||
# Tables and Structs (AKA: "maps", "hashmaps", "dictionaries")
|
# Tables and Structs (associative data structures)
|
||||||
@{:a 1 :b 2 :c 3} # table (mutable)
|
@{:a 1 :b 2 :c 3} # table (mutable)
|
||||||
{:a 1 :b 2 :c 3} # struct (immutable)
|
{:a 1 :b 2 :c 3} # struct (immutable)
|
||||||
|
|
||||||
|
# To "pretty-print" these out, use `pp` instead of `print`.
|
||||||
# More about how to work with arrays/tuples and tables/structs below.
|
# More about how to work with arrays/tuples and tables/structs below.
|
||||||
|
|
||||||
# Bindings ####################################################################
|
# Bindings ####################################################################
|
||||||
# ... or "Name Some Things!" (that is, bind a value to a symbol)
|
# Bind a value to a symbol.
|
||||||
(def x 4.7) # Define a constant, `x`.
|
(def x 4.7) # Define a constant, `x`.
|
||||||
x # => 4.7
|
x # => 4.7
|
||||||
(quote x) # => x (the symbol x)
|
(quote x) # => x (the symbol x)
|
||||||
@@ -113,7 +116,7 @@ x # => 4.7
|
|||||||
(set x 5.6) # Error, `x` is a constant.
|
(set x 5.6) # Error, `x` is a constant.
|
||||||
|
|
||||||
(var y 10)
|
(var y 10)
|
||||||
(set y 12) # Works, since `y` was made var.
|
(set y 12) # Works, since `y` was defined using `var`.
|
||||||
|
|
||||||
# Note that bindings are local to the scope they're called in. `let`
|
# Note that bindings are local to the scope they're called in. `let`
|
||||||
# creates a local scope and makes some bindings all in one shot:
|
# creates a local scope and makes some bindings all in one shot:
|
||||||
@@ -151,19 +154,21 @@ insect-friend # => bee
|
|||||||
(% 5 3) # => 2 (remainder)
|
(% 5 3) # => 2 (remainder)
|
||||||
(- 5) # => -5 (or you can just write `-5`)
|
(- 5) # => -5 (or you can just write `-5`)
|
||||||
|
|
||||||
(++ i) # increments
|
(++ i) # increments (modifies `i`)
|
||||||
(-- i) # decrements
|
(-- i) # decrements
|
||||||
(+= i 3) # add 3 to `i`
|
(+= i 3) # add 3 to `i`
|
||||||
(*= i 3) # triple `i`
|
(*= i 3) # triple `i`
|
||||||
# ... and so on for the other operations on numbers.
|
# ... and so on for the other operations on numbers.
|
||||||
|
|
||||||
|
# If you don't want to mutate `i`, use `(inc i)` and `(dec i)`.
|
||||||
|
|
||||||
# Comparison
|
# Comparison
|
||||||
# = < > not= <= >=
|
# = < > not= <= >=
|
||||||
(< 2 7 12) # => true
|
(< 2 7 12) # => true
|
||||||
|
|
||||||
# Functions ###################################################################
|
# Functions ###################################################################
|
||||||
# Call them:
|
# Call them:
|
||||||
(- 5 3) # => 2 (Yes, operators and functions work the same.)
|
(- 5 3) # => 2 (Operators and functions work the same way.)
|
||||||
(math/sin (/ math/pi 2)) # => 1
|
(math/sin (/ math/pi 2)) # => 1
|
||||||
(range 5) # => @[0 1 2 3 4]
|
(range 5) # => @[0 1 2 3 4]
|
||||||
|
|
||||||
@@ -171,9 +176,7 @@ insect-friend # => bee
|
|||||||
(defn mult-by-2
|
(defn mult-by-2
|
||||||
``First line of docstring.
|
``First line of docstring.
|
||||||
|
|
||||||
Some more of the docstring.
|
Some more of the docstring.``
|
||||||
|
|
||||||
Possibly more!``
|
|
||||||
[x]
|
[x]
|
||||||
(print "Hi.")
|
(print "Hi.")
|
||||||
(print "Will compute using: " x)
|
(print "Will compute using: " x)
|
||||||
@@ -206,7 +209,7 @@ n # => 3
|
|||||||
# You might say that function bodies provide an "implicit do".
|
# You might say that function bodies provide an "implicit do".
|
||||||
|
|
||||||
# Operations on data structures ###############################################
|
# Operations on data structures ###############################################
|
||||||
# (Making all these mutable so we can ... mutate them.)
|
# (Making all of these mutable so we can ... mutate them.)
|
||||||
(def s @"Hello, World!")
|
(def s @"Hello, World!")
|
||||||
(def a @[:a :b :c :d :e])
|
(def a @[:a :b :c :d :e])
|
||||||
(def t @{:a 1 :b 2})
|
(def t @{:a 1 :b 2})
|
||||||
@@ -227,14 +230,14 @@ n # => 3
|
|||||||
(put a 2 :x) # @[:a :b :x :d :e]
|
(put a 2 :x) # @[:a :b :x :d :e]
|
||||||
(put t :b 42) # @{:a 1 :b 42}
|
(put t :b 42) # @{:a 1 :b 42}
|
||||||
|
|
||||||
# Adding & removing values (again, for mutable data structures):
|
# Adding and removing values (again, for mutable data structures):
|
||||||
(buffer/push-string s "??") # @"HeWlo, World!??"
|
(buffer/push-string s "??") # @"HeWlo, World!??"
|
||||||
(array/push a :f) # @[:a :b :x :d :e :f]
|
(array/push a :f) # @[:a :b :x :d :e :f]
|
||||||
(array/pop a) # => :f, and it's also removed from `a`.
|
(array/pop a) # => :f, and it's also removed from `a`.
|
||||||
(put t :x 88) # @{:a 1 :b 42 :x 88}
|
(put t :x 88) # @{:a 1 :b 42 :x 88}
|
||||||
|
|
||||||
# See the manual for a wide variety of functions for working with
|
# See the manual for a wide variety of functions for working with
|
||||||
# buffers/strings, arrays/tuples, and tables/struct.
|
# buffers/strings, arrays/tuples, and tables/structs.
|
||||||
|
|
||||||
# Flow control ################################################################
|
# Flow control ################################################################
|
||||||
(if some-condition
|
(if some-condition
|
||||||
|
Reference in New Issue
Block a user