aboutsummaryrefslogtreecommitdiffhomepage
path: root/std/String.bruijn
blob: 76121ee16f6bdf76719192fceb2d70a008b1cae9 (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
# 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)

# 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
string→number from-digits ∘ (map C.char→number) ⧗ String → Number

:test (%(string→number "123")) ((+123))

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

:test (%(string→signed-number "+123")) ((+123))
:test (%(string→signed-number "-123")) ((-123))

# converts a number to a string
number→string (map C.number→char) ∘ digits ⧗ Number → String

:test (number→string (+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")