1
0
mirror of https://github.com/adambard/learnxinyminutes-docs.git synced 2025-08-06 14:56:54 +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:
John Gabriele
2023-12-14 11:27:13 -05:00
committed by GitHub
parent 51900f1838
commit 58d91b0ba3

View File

@@ -58,7 +58,8 @@ math/e # => 2.71828
"hello"
"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
multi-line
string`` # => "a long\nmulti-line\nstring"
@@ -81,7 +82,8 @@ one here`
# See the string library for more (splitting, replacement, etc.)
# Arrays and Tuples ###########################################################
# Data Structures #############################################################
# Arrays and Tuples
# Arrays are mutable, tuples are immutable.
# Arrays (mutable)
@@ -91,18 +93,19 @@ one here`
# Tuples (immutable)
# 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
# quote mark).
# quote mark)...
'(4 5 6)
[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} # struct (immutable)
# To "pretty-print" these out, use `pp` instead of `print`.
# More about how to work with arrays/tuples and tables/structs below.
# 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`.
x # => 4.7
(quote x) # => x (the symbol x)
@@ -113,7 +116,7 @@ x # => 4.7
(set x 5.6) # Error, `x` is a constant.
(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`
# creates a local scope and makes some bindings all in one shot:
@@ -151,19 +154,21 @@ insect-friend # => bee
(% 5 3) # => 2 (remainder)
(- 5) # => -5 (or you can just write `-5`)
(++ i) # increments
(++ i) # increments (modifies `i`)
(-- i) # decrements
(+= i 3) # add 3 to `i`
(*= i 3) # triple `i`
# ... and so on for the other operations on numbers.
# If you don't want to mutate `i`, use `(inc i)` and `(dec i)`.
# Comparison
# = < > not= <= >=
(< 2 7 12) # => true
# Functions ###################################################################
# 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
(range 5) # => @[0 1 2 3 4]
@@ -171,9 +176,7 @@ insect-friend # => bee
(defn mult-by-2
``First line of docstring.
Some more of the docstring.
Possibly more!``
Some more of the docstring.``
[x]
(print "Hi.")
(print "Will compute using: " x)
@@ -206,7 +209,7 @@ n # => 3
# You might say that function bodies provide an "implicit do".
# 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 a @[:a :b :c :d :e])
(def t @{:a 1 :b 2})
@@ -227,14 +230,14 @@ n # => 3
(put a 2 :x) # @[:a :b :x :d :e]
(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!??"
(array/push a :f) # @[:a :b :x :d :e :f]
(array/pop a) # => :f, and it's also removed from `a`.
(put t :x 88) # @{:a 1 :b 42 :x 88}
# 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 ################################################################
(if some-condition