Skip to content

Commit

Permalink
Lib: list::length -> list::size, add list::map, string::length, strin…
Browse files Browse the repository at this point in the history
…g::split, string::trim
  • Loading branch information
ChiriVulpes committed Dec 14, 2024
1 parent 435c163 commit e41cbaa
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 3 deletions.
3 changes: 2 additions & 1 deletion lib/function/list.chiri
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#import:
list/length
list/size
list/join
list/map
9 changes: 9 additions & 0 deletions lib/function/list/map.chiri
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#function map with t!* r!* returns list!r:
#list!t values
#function!(t uint list!t r) mapper

#list!r result = [
#each values as var i, var value:
#set result = [...result, mapper(value, i, values)

#return result
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#function length returns uint:
#function size returns uint:
#list!* values

#uint result = 0
Expand Down
6 changes: 5 additions & 1 deletion lib/function/string.chiri
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
#import: string/hex
#import:
string/length
string/hex
string/split
string/trim
8 changes: 8 additions & 0 deletions lib/function/string/length.chiri
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#function length returns uint:
#string input

#uint result = 0
#each input as var char:
#set result++

#return result
20 changes: 20 additions & 0 deletions lib/function/string/split.chiri
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#function split returns list!string:
#string target
#string separator ??= "
#list!string result = [

#if separator == "":
#each in target as string char:
#set result = [...result, char

#else:
#uint l = separator::length
#uint s = 0
#each in target as uint i, string char:
#uint e = i + l
#if target[i..e] == separator:
#set result = [...result, target[s..i]
#set s = i + l
#set result = [...result, target[s...]

#return result
28 changes: 28 additions & 0 deletions lib/function/string/trim.chiri
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#function trim returns string:
#string input

#uint length = input::length
#uint start = 0
#uint end = input::length

#while start < length:
#string char = input[start]
#if char == " " || char == "\t" || char == "\r" || char == "\n":
#set start++
#continue

#break

#while end > start:
#string char = input[end - 1]
#if char == " " || char == "\t" || char == "\r" || char == "\n":
#set end--
#continue

#break

#string result = ""
#for var i = start, i < end, set i++:
#set result = result . input[i]

#return result

0 comments on commit e41cbaa

Please sign in to comment.