blob: 6ffd1f4895664ca0a74d1c09b9dec687e9d94fc3 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
# MIT License, Copyright (c) 2022 Marvin Borner
:import std/Char C
:import std/Math .
:import std/Number/Binary B
:input std/List
# returns true if two strings are the same
eq? eq? B.eq? ⧗ String → String → Boolean
…=?… eq?
:test ("ab" =? "ab") (true)
:test ("ab" =? "aa") (false)
# prefix for comparing functions
?‣ &eq?
# returns eq, gt, lt depending on comparison of two numbers
compare-case B.<=>compare-case ⧗ a → b → c → String → String → d
# returns 1 if a>b, -1 if a<b and 0 if a=b
# also: spaceship operator
compare compare-case (+0) (+1) (-1) ⧗ String → String → Number
…<=>… compare
<=>‣ &compare
:test (compare "2" "2") ((+0))
:test (compare "2" "1") ((+1))
:test (compare "1" "2") ((-1))
:test (compare "12" "1") ((-1))
:test (compare "1" "12") ((+1))
# returns true if string is lexically less than other string
les? c-les? ∘∘ compare ⧗ String → String → Boolean
…<?… les?
:test ("1" <? "2") (true)
:test ("2" <? "2") (false)
:test ("3" <? "2") (false)
# returns true if string is lexically greater than other string
gre? \les? ⧗ String → String → Boolean
…>?… gre?
:test ("1" >? "2") (false)
:test ("2" >? "2") (false)
:test ("3" >? "2") (true)
# returns true if string is lexically less than or equal to other string
leq? not! ∘∘ gre? ⧗ String → String → Boolean
…≤?… leq?
:test ("1" ≤? "2") (true)
:test ("2" ≤? "2") (true)
:test ("3" ≤? "2") (false)
# returns true if number is greater than or equal to other string
geq? \leq? ⧗ String → String → Boolean
…≥?… geq?
:test ("1" ≥? "2") (false)
:test ("2" ≥? "2") (true)
:test ("3" ≥? "2") (true)
# returns true if character is part of a string
in? B.?in? ⧗ Char → String → Boolean
…∈… in?
ni? \in? ⧗ String → Char → Boolean
…∋… ni?
:test ('b' ∈ "ab") (true)
:test ('c' ∈ "ab") (false)
:test ("ab" ∋ 'b') (true)
:test ("ab" ∋ 'c') (false)
# converts a string of digits into a number
string→unsigned-number list→number ∘ (map C.char→number) ⧗ String → Number
:test (%(string→unsigned-number "123")) ((+123))
# converts a signed string of digits into a number
string→signed-number [(sign ^0) (string→unsigned-number ~0)] ⧗ String → Number
sign [(B.eq? 0 '-') -‣ i]
:test (%(string→signed-number "+123")) ((+123))
:test (%(string→signed-number "-123")) ((-123))
# converts signed/unsigned number strings to a number
string→number [C.les? ^0 '0' signed unsigned] ⧗ String → Number
signed string→signed-number 0
unsigned string→unsigned-number 0
:test (%(string→number "123")) ((+123))
:test (%(string→number "+123")) ((+123))
:test (%(string→number "-123")) ((-123))
# converts a number to a string
number→string (map C.number→char) ∘ number→list ⧗ Number → String
:test (number→string (+123)) ("123")
# trims whitespace from the beginning and end of a string
trim w b (reverse ∘ (drop-while (ni? "\t\r\n "))) ⧗ String → String
:test (trim " ab ") ("ab")
:test (trim "\t\r\nab\t\r\n") ("ab")
# splits string by newline character
lines z [[rec]] ⧗ String → (List String)
rec build (break (B.eq? '\n') 0)
build [~0 [[[^3 : (5 1)]]] {}(^0) ]
:test (lines "ab\ncd") ("ab" : {}"cd")
# concats list of strings with newline character
unlines concat-map (\(…;…) '\n') ⧗ (List String) → String
:test (unlines ("ab" : {}"cd")) ("ab\ncd\n")
|