Skip to content

Commit

Permalink
Merge pull request #840 from candy-lang/more-text-functions
Browse files Browse the repository at this point in the history
More text functions
  • Loading branch information
jwbot authored Jan 6, 2024
2 parents a0a09eb + d7e0562 commit 43e93ad
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 17 deletions.
40 changes: 40 additions & 0 deletions packages/Core/list.candy
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,46 @@ concatenate listA listB :=
}
}

firstIndexWhere list predicate :=
needs (is list)
needs (function.is1 predicate)
recursive [list, Index: 0] { recurse [list, index] ->
ifElse (index | equals (list | length)) { NotFound } {
predicateResult = predicate (list | get index)
needs (bool.is predicateResult)
ifElse predicateResult { FoundAt index } { recurse [list, Index: int.add index 1] }
}
}
firstWhere list predicate :=
needs (is list)
needs (function.is1 predicate)
list | firstIndexWhere predicate %
FoundAt index -> Found (list | get index)
NotFound -> NotFound
firstIndexOf list item :=
needs (is list)
firstIndexWhere list { a -> a | equals item }
lastIndexWhere list predicate :=
needs (is list)
needs (function.is1 predicate)
recursive [list, Index: list | lastIndex | result.unwrapOr (int.negate 1)] {
recurse [list, index] ->
ifElse (index | equals (int.negate 1)) { NotFound } {
predicateResult = predicate (list | get index)
needs (bool.is predicateResult)
ifElse predicateResult { FoundAt index } { recurse [list, Index: int.subtract index 1] }
}
}
lastWhere list predicate :=
needs (is list)
needs (function.is1 predicate)
list | lastIndexWhere predicate %
FoundAt index -> Found (list | get index)
NotFound -> NotFound
lastIndexOf list item :=
needs (is list)
lastIndexWhere list { a -> a | equals item }

#test =
# [checkEquals] = use "..check"
#
Expand Down
53 changes: 36 additions & 17 deletions packages/Core/text.candy
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ builtins = use "Builtins"
bool = use "..bool"
[ifElse, recursive] = use "..controlFlow"
[equals] = use "..equality"
function = use "..function"
int = use "..int"
# iterable = use "..iterable"
list = use "..list"
Expand All @@ -10,6 +11,9 @@ type = use "..type"

is value := type.is value Text

newline := "

"
rick := "
We're no strangers to love
You know the rules and so do I
Expand Down Expand Up @@ -122,16 +126,31 @@ trim text :=
needs (is text)
text | trimStart | trimEnd

firstIndexOf text char :=
firstIndexWhere text predicate :=
needs (is text)
needs (is char)
needs (length char | equals 1)
recursive 0 { recurse i ->
ifElse
text | length | int.isGreaterThan i
{ ifElse (text | characterAt i | equals char) { i } { i | int.add 1 | recurse } }
{ int.negate 1 }
}
needs (function.is1 predicate)
text | characters | list.firstIndexWhere predicate
firstIndexOf text character :=
needs (is text)
needs (is character)
needs (character | length | equals 1)
text | characters | list.firstIndexOf character
lastIndexWhere text predicate :=
needs (is text)
needs (function.is1 predicate)
text | characters | list.lastIndexWhere predicate
lastIndexOf text character :=
needs (is text)
needs (is character)
needs (character | length | equals 1)
text | characters | list.lastIndexOf character

isAsciiDigit character :=
needs (is character)
needs (character | length | equals 1)
character %
"0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" -> True
_ -> False

repeat text times :=
needs (is text)
Expand All @@ -143,23 +162,23 @@ repeat text times :=
}
}

padStart text len char :=
padStart text len character :=
needs (is text)
needs (int.is len)
needs (is char)
needs (char | length | equals 1)
needs (is character)
needs (character | length | equals 1)
textLength = text | length
ifElse (textLength | int.isGreaterThanOrEqualTo len) { text } {
(char | repeat (len | int.subtract textLength)) | concatenate text
(character | repeat (len | int.subtract textLength)) | concatenate text
}
padEnd text len char :=
padEnd text len character :=
needs (is text)
needs (int.is len)
needs (is char)
needs (char | length | equals 1)
needs (is character)
needs (character | length | equals 1)
textLength = text | length
ifElse (textLength | int.isGreaterThanOrEqualTo len) { text } {
text | concatenate (char | repeat (len | int.subtract textLength))
text | concatenate (character | repeat (len | int.subtract textLength))
}

#test =
Expand Down

1 comment on commit 43e93ad

@jwbot
Copy link
Collaborator Author

@jwbot jwbot commented on 43e93ad Jan 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compiler

Benchmark suite Current: 43e93ad Previous: a0a09eb Ratio
Time: Compiler/hello_world 18696214 ns/iter (± 663107) 17625632 ns/iter (± 707868) 1.06
Time: Compiler/fibonacci 144743100 ns/iter (± 1008251) 138013495 ns/iter (± 663091) 1.05
Time: VM Runtime/hello_world 32055 ns/iter (± 92728) 32010 ns/iter (± 1622) 1.00

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.