aboutsummaryrefslogtreecommitdiffhomepage
path: root/std/String.bruijn
blob: 5ea6ef9abc64305b7e55fccec23b897cd961f343 (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
# MIT License, Copyright (c) 2022 Marvin Borner

:import std/Char C
:import std/Number .
: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)

# returns true if character is part of a string
in? in? B.eq? ⧗ 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
number! from-digits ∘ (map C.number!) ⧗ String → Number

:test (%(number! "123")) ((+123))

# converts a signed string of digits into a number
signed-number! [(sign ^0) (number! ~0)] ⧗ String → Number
	sign [(B.eq? 0 '-') -‣ i]

:test (%(signed-number! "+123")) ((+123))
:test (%(signed-number! "-123")) ((-123))

# 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")