Rye reference: Misc

Type conversion and checking

Functions that convert between Rye value types.

to-word

Pure Builtin(1): Tries to change a Rye value to a word with same name.

to-word "test"
; returns test
to-word 'test
; returns test

to-integer

Builtin(1): Tries to change a Rye value (like string) to integer.

to-integer "123"
; returns 123

to-string

Pure Builtin(1): Tries to turn a Rye value to string.

to-string 123
; returns "123"
to-string 'word
; returns "word"

to-uri

Pure Builtin(1): Tries to change Rye value to an URI.

to-uri "https://example.com"
; returns https://example.com

to-file

Pure Builtin(1): Tries to change Rye value to a file.

to-file "file.txt"
; returns file://file.txt

to-context

Builtin(1): Takes a Dict and returns a Context with same names and values.

cc: to-context dict { "name" "Jim" } , cc
; returns "Jim"

is-integer

Pure Builtin(1): Returns true if value is an integer.

is-integer 123
; returns 1
is-integer "ABC"
; returns 0

is-decimal

Pure Builtin(1): Returns true if value is a decimal.

is-decimal 123.456000
; returns 1
is-decimal 123
; returns 0
is-decimal "ABC"
; returns 0

is-number

Pure Builtin(1): Returns true if value is a number (integer or decimal).

is-number 123
; returns 1
is-number 123.456000
; returns 1
is-number "ABC"
; returns 0

is-string

Pure Builtin(1): Returns true if value is a string.

is-string "ABC"
; returns 1
is-string 123
; returns 0
is-string file://file.txt
; returns 0
is-string test@gmail.com
; returns 0

type?

Pure Builtin(1): Returns the type of Rye value as a word.

type? "ABC"
; returns string
type? 123
; returns integer

Failure handling functions

Functions for handling and working with Context.

fail

Builtin(1): Constructs and Fails with an Error object. Accepts String as message, Integer as code, or block for multiple parameters.

fail 404 |disarm |code?
; returns 404
fail "message" |disarm |mold
; returns "Error: message "
fail { 501 "auth error" } |disarm |mold
; returns "Error(501): auth error "

failure

Builtin(1): Constructs and Error object. Accepts String as message, Integer as code, or block for multiple parameters.

failure 403 |type?
; returns error

code?

Builtin(1): Constructs and Fails with an Error object. Accepts String as message, Integer as code, or block for multiple parameters.

failure 403 |code?
; returns 403

disarm

Builtin(1): Constructs and Fails with an Error object. Accepts String as message, Integer as code, or block for multiple parameters.

failure 403 |disarm
; returns Error(403):  

failed?

Builtin(1): Constructs and Fails with an Error object. Accepts String as message, Integer as code, or block for multiple parameters.

fail 403 |failed?
; returns 1

check

Builtin(1): Constructs and Fails with an Error object. Accepts String as message, Integer as code, or block for multiple parameters.

1 / 0 |check 501 |code?
; returns 501

fix

Builtin(1): Constructs and Fails with an Error object. Accepts String as message, Integer as code, or block for multiple parameters.

100 / 0 |fix { 99 } |_+ 1
; returns 100
100 / 1 |fix { 99 } |_+ 1
; returns 101

assert-equal

Pure Builtin(2): Test if two values are equal. Fail if not.

assert-equal 10 10
; returns 1
assert-equal 10 20 |disarm |type?
; returns error

Code flow combinators

Program flow control functions

pass

Builtin(2): Accepts a value and a block. It does the block, with value injected, and returns (passes on) the initial value.

"xxx" .pass { + "yyy" :xy }
; returns "xxx"

keep

Builtin(3): Do the first block, then the second one but return the result of the first one.

10 .keep { + 1 } { * 10 }
; returns 11

wrap

Builtin(2): Accepts a value and a block. It does the block, with value injected, and returns (passes on) the initial value.

wrap { 1 + 1 } { 10 + 20 }
; returns 30

Functions that change values in-place

inc!

Builtin(1): Searches for a word and increments it's integer value in-place.

a: 100 , inc! 'a
; returns 101
a: 100 , inc! 'a , a
; returns 101

change!

Builtin(2): Searches for a word and changes it's value in-place. If value changes returns true otherwise false

a: 1 change! 2 'a
; returns 1
a: 2 change! 2 'a
; returns 0

Functions that change values in-place are used more rarely in Rye and have ! at the end.

remove-last!

Builtin(1): Accepts Block and returns the next value and removes it from the Block.

b: { 1 2 3 } , remove-last! 'b , b
; returns { 1 2 }

append!

Builtin(2): Accepts Rye value and Tagword with a Block or String. Appends Rye value to Block/String in place, also returns it .

b: { 1 2 3 } , append! 4 'b , b
; returns { 1 2 3 4 }
b: { 1 2 3 } , append! { 4 5 } 'b , b
; returns { 1 2 3 { 4 5 } }

change\ nth!

Builtin(3): Accepts a Block or List, Integer n and a value. Changes the n-th value in the Block in place. Also returns the new series.

b: { 1 4 3 } , change\nth! b 2 4
; returns { 1 4 3 }
b: { 1 { 4 5 } 3 } , change\nth! b 2 { 4 5 }
; returns { 1 { 4 5 } 3 }
b: list { 1 2 3 } , change\nth! b 2 4
; returns L[ 1  4  3 ]
b: list { 1 2 3 } , change\nth! b 2 list { 4 5 }
; returns L[ 1  L[ 4  5 ]  3 ]
try { b: { 1 2 3 } , change\nth! b 4 0 } |type?
; returns error
try { b: list { 1 2 3 } , change\nth! b 4 0 } |type?
; returns error

sort!

Builtin(2): Accepts Rye value and Tagword with a Block or String. Appends Rye value to Block/String in place, also returns it .

b: { 1 2 4 7 } , sort! b , b
; returns { 1 2 4 7 }

reverse!

Builtin(2): Accepts Rye value and Tagword with a Block or String. Appends Rye value to Block/String in place, also returns it .

b: { 2 7 1 4 } , reverse! b , b
; returns { 2 7 1 4 }

Date and time functions

date

Builtin(1): Accepts a String and returns a Date object.

date "2023-01-01" |type?
; returns date
try { date "not-date" } |type?
; returns error

datetime

Builtin(1): Accepts a String and returns a Date object.

datetime "2023-01-01T12:00:00" |type?
; returns time
try { datetime "not-time" } |type?
; returns error

now

Builtin(0): Returns current Time.

now |type?
; returns time