Skip to content

Latest commit

 

History

History
565 lines (509 loc) · 7.51 KB

base.md

File metadata and controls

565 lines (509 loc) · 7.51 KB

base.elv

  1. testing-status
  2. is-zero
  3. is-one
  4. evens
  5. odds
  6. inc
  7. dec
  8. pos/neg
  9. is-functions
  10. prepend
  11. append
  12. concat2
  13. pluck
  14. get
  15. first
  16. ffirst
  17. second
  18. rest
  19. end
  20. butlast
  21. is-empty
  22. swap
  23. nth
  24. check-pipe
  25. flatten

testing-status

96 tests passed out of 96

100% of tests are passing

These functions largely assume numbers, lists, and strings. The list operations are of dubious usefulness for users, however.

Math functions


is-zero

works with text, nums, and inexact-nums

is-zero 0
is-zero (num 0)
is-zero (inexact-num 0)
$true
is-zero 1
is-zero (randint 1 100)
is-zero (inexact-num (randint 1 100))
$false

is-one

works with text, nums, and inexact-nums

is-one 1
is-one (num 1)
is-one (inexact-num 1)
$true
is-one 0
is-one (num 0)
is-one (inexact-num 0)
$false

evens

only works with strings & nums

range -5 6 | each $is-even~
range -5 6 | each $to-string~ | each $is-even~
$false$true$false$true$false$true$false$true$false$true$false

fails with inexact-nums

is-even 5.0
▶ [&reason=<unknown wrong type for arg #0: cannot parse as integer: 5.0> &stack-trace=<...>]

odds

only works with strings & nums

range -5 6 | each $is-odd~
range -5 6 | each $to-string~ | each $is-odd~
$true$false$true$false$true$false$true$false$true$false$true

fails with inexact-nums

is-odd 5.0
▶ [&reason=<unknown wrong type for arg #0: cannot parse as integer: 5.0> &stack-trace=<...>]

inc

works with text, nums, and inexact-nums

range -5 6 | each $inc~
range -5 6 | each $to-string~ | each $inc~
▶ -4
▶ -3
▶ -2
▶ -1
▶ 0
▶ 1
▶ 2
▶ 3
▶ 4
▶ 5
▶ 6
range -5 6 | each $inexact-num~ | each $inc~
▶ -4.0
▶ -3.0
▶ -2.0
▶ -1.0
▶ 0.0
▶ 1.0
▶ 2.0
▶ 3.0
▶ 4.0
▶ 5.0
▶ 6.0

dec

works with text, nums, and inexact-nums

range -5 6 | each $dec~
range -5 6 | each $to-string~ | each $dec~
▶ -6
▶ -5
▶ -4
▶ -3
▶ -2
▶ -1
▶ 0
▶ 1
▶ 2
▶ 3
▶ 4
range -5 6 | each $inexact-num~ | each $dec~
▶ -6.0
▶ -5.0
▶ -4.0
▶ -3.0
▶ -2.0
▶ -1.0
▶ 0.0
▶ 1.0
▶ 2.0
▶ 3.0
▶ 4.0

pos/neg

works with text, nums, and inexact-nums

each $pos~ [-1 1]
each $neg~ [1 -1]
each $pos~ [(num -1) (num 1)]
each $neg~ [(num 1) (num -1)]
each $pos~ [(inexact-num -1) (inexact-num 1)]
each $neg~ [(inexact-num 1) (inexact-num -1)]
$false$true

Type predicates


is-functions

predicate functions for types

is-fn { }
is-map [&]
is-list []
is-bool $true
is-number (num 0)
is-string ""
$true

lots of things which look like other types are actually strings

is-string 1
is-string {}
$true

likewise, these look like a number and a function, but they are actually strings

is-number 1
is-fn {}
$false

List operations


prepend

prepends a scalar value to a list

prepend [2 3] 0 1
put [2 3] | prepend (all) 0 1
put 2 3 | prepend [(all)] 0 1
[0 1 2 3]

prepend on strings implicitly transforms to list

prepend ello h
▶ [h e l l o]

append

appends a scalar value to a list

append [0 1] 2 3
put [0 1] | append (all) 2 3
put 0 1 | append [(all)] 2 3
[0 1 2 3]

append on strings implicitly transforms to list

append hell o
▶ [h e l l o]

concat2

concatenate two lists

concat2 [0 1] [2 3][0 1 2 3]

concat2 on strings implicitly transforms to list

concat2 he llo
▶ [h e l l o]

pluck

removes the element at a given index from a list.

pluck [0 1 x 2 3] 2
put [0 1 x 2 3] | pluck (all) 2
put 0 1 x 2 3 | pluck [(all)] 2
[0 1 2 3]

corner-cases

put [-1 0 1 2 3] | pluck (all) 0
put [0 1 2 3 4] | pluck (all) 4
[0 1 2 3]

pluck on strings implicitly transforms to list

pluck x-men 1
▶ [x m e n]

get

retrieves the element at index i in a list

get [0 1 s 2 3] 2
put [0 1 s 2 3] | get (all) 2
put 0 1 s 2 3 | get [(all)] 2
▶ s

works on strings, too

get string 0
▶ s

first

retrieves the first element from a list

first [0 1 2 3]
put 0 1 2 3 | first [(all)]
▶ 0

works on strings, too

first "hello"
first hello
▶ h

ffirst

nested first on a list

ffirst [[a b c] 1 2 3]
put [a b c] 1 2 3 | ffirst [(all)]
▶ a

second

retrieves the second element from a list

second [0 1 2 3]
put 0 1 2 3 | second [(all)]
▶ 1

works on strings, too

second "hello"
second hello
▶ e

rest

drops the first element from a list

rest [0 1 2 3]
put 0 1 2 3 | rest [(all)]
[1 2 3]

works on strings without coercing the result to a list

rest "hello"
rest hello
▶ ello

end

retrieves the last element from a list (the end of a list)

end [0 1 2 3]
put 0 1 2 3 | end [(all)]
▶ 3

works on strings, too

end "hello"
end hello
▶ o

butlast

drops the last element from a list

butlast [0 1 2 3]
put 0 1 2 3 | butlast [(all)]
[0 1 2]

works on strings without coercing the result to a list

butlast "hello"
butlast hello
▶ hell

is-empty

does whats on the tin

is-empty []
is-empty ''
$true

swap

Works on maps

swap [&a=2 &b=1] a b
▶ [&a=1 &b=2]

Works on lists

swap [b a c] 0 1
▶ [a b c]

Works on strings

swap

tsuff

0

1

More complicated list operations


nth

returns the nth item in a list

nth [f o o b a r] 3
put f o o b a r | nth [(all)] 3
▶ b

and of course it works with strings

nth foobar 3
▶ b

It returns nothing if the index is out of range

nth [f o o b a r] 10

MATCHES EXPECTATIONS: [nothing]

You can optionally specify the not-found value

nth [$nil $nil $nil] 10 &not-found=kaboom
▶ kaboom

It uses drop under the hood, so negative indices just return the 0-index

nth [f o o b a r] -10
▶ f

check-pipe

this is probably the most interesting function here. it takes input, and if the input is empty, returns whats in the pipe. Otherwise it returns the input, exploded.

check-pipe [1 2 3]
put 1 2 3 | check-pipe []
▶ 1
▶ 2
▶ 3

flatten

recursive function which basically performs nested explosions on a list, ignoring lists.

flatten [1 [2 3] [4 [[5 [6] 7]] 8 [] [9]]]
▶ 1
▶ 2
▶ 3
▶ 4
▶ 5
▶ 6
▶ 7
▶ 8
▶ 9

anything else is just returned

flatten foobar
▶ foobar