mirror of
https://github.com/roc-lang/roc.git
synced 2025-08-02 19:32:17 +00:00
completions working
This commit is contained in:
parent
0c463555f4
commit
d95da9dd5b
4 changed files with 165 additions and 105 deletions
|
@ -80,7 +80,7 @@ interface List
|
|||
## ## Types
|
||||
##
|
||||
## A sequential list of values.
|
||||
## ```
|
||||
## ```roc
|
||||
## [1, 2, 3] # a list of numbers
|
||||
## ["a", "b", "c"] # a list of strings
|
||||
## [[1.1], [], [2.2, 3.3]] # a list of lists of numbers
|
||||
|
@ -112,7 +112,7 @@ interface List
|
|||
## will be immediately freed.
|
||||
##
|
||||
## Let's look at an example.
|
||||
## ```
|
||||
## ```roc
|
||||
## ratings = [5, 4, 3]
|
||||
##
|
||||
## { foo: ratings, bar: ratings }
|
||||
|
@ -125,7 +125,7 @@ interface List
|
|||
## refcount getting incremented from 1 to 3.
|
||||
##
|
||||
## Let's turn this example into a function.
|
||||
## ```
|
||||
## ```roc
|
||||
## getRatings = \first ->
|
||||
## ratings = [first, 4, 3]
|
||||
##
|
||||
|
@ -147,7 +147,7 @@ interface List
|
|||
## list, and that list has a refcount of 2.
|
||||
##
|
||||
## Let's change the last line to be `(getRatings 5).bar` instead of `getRatings 5`:
|
||||
## ```
|
||||
## ```roc
|
||||
## getRatings = \first ->
|
||||
## ratings = [first, 4, 3]
|
||||
##
|
||||
|
@ -161,7 +161,7 @@ interface List
|
|||
## where it started: there is only 1 reference to it.
|
||||
##
|
||||
## Finally let's suppose the final line were changed to this:
|
||||
## ```
|
||||
## ```roc
|
||||
## List.first (getRatings 5).bar
|
||||
## ```
|
||||
## This call to [List.first] means that even the list in the `bar` field has become
|
||||
|
@ -174,7 +174,7 @@ interface List
|
|||
## and then with a list of lists, to see how they differ.
|
||||
##
|
||||
## Here's the example using a list of numbers.
|
||||
## ```
|
||||
## ```roc
|
||||
## nums = [1, 2, 3, 4, 5, 6, 7]
|
||||
##
|
||||
## first = List.first nums
|
||||
|
@ -185,7 +185,7 @@ interface List
|
|||
## It makes a list, calls [List.first] and [List.last] on it, and then returns `first`.
|
||||
##
|
||||
## Here's the equivalent code with a list of lists:
|
||||
## ```
|
||||
## ```roc
|
||||
## lists = [[1], [2, 3], [], [4, 5, 6, 7]]
|
||||
##
|
||||
## first = List.first lists
|
||||
|
@ -216,7 +216,7 @@ interface List
|
|||
# separator so List.isEmpty doesn't absorb the above into its doc comment
|
||||
|
||||
## Check if the list is empty.
|
||||
## ```
|
||||
## ```roc
|
||||
## List.isEmpty [1, 2, 3]
|
||||
##
|
||||
## List.isEmpty []
|
||||
|
@ -232,7 +232,7 @@ getUnsafe : List a, U64 -> a
|
|||
## Returns an element from a list at the given index.
|
||||
##
|
||||
## Returns `Err OutOfBounds` if the given index exceeds the List's length
|
||||
## ```
|
||||
## ```roc
|
||||
## expect List.get [100, 200, 300] 1 == Ok 200
|
||||
## expect List.get [100, 200, 300] 5 == Err OutOfBounds
|
||||
## ```
|
||||
|
@ -255,7 +255,7 @@ replace = \list, index, newValue ->
|
|||
{ list, value: newValue }
|
||||
|
||||
## Replaces the element at the given index with a replacement.
|
||||
## ```
|
||||
## ```roc
|
||||
## List.set ["a", "b", "c"] 1 "B"
|
||||
## ```
|
||||
## If the given index is outside the bounds of the list, returns the original
|
||||
|
@ -267,7 +267,7 @@ set = \list, index, value ->
|
|||
(List.replace list index value).list
|
||||
|
||||
## Updates the element at the given index with the given function.
|
||||
## ```
|
||||
## ```roc
|
||||
## List.update [1, 2, 3] 1 (\x -> x + 1)
|
||||
## ```
|
||||
## If the given index is outside the bounds of the list, returns the original
|
||||
|
@ -311,7 +311,7 @@ expect
|
|||
got == want
|
||||
|
||||
## Add a single element to the end of a list.
|
||||
## ```
|
||||
## ```roc
|
||||
## List.append [1, 2, 3] 4
|
||||
##
|
||||
## [0, 1, 2]
|
||||
|
@ -326,7 +326,7 @@ append = \list, element ->
|
|||
## If the given [Result] is `Ok`, add it to the end of a list.
|
||||
## Otherwise, return the list unmodified.
|
||||
##
|
||||
## ```
|
||||
## ```roc
|
||||
## List.appendIfOk [1, 2, 3] (Ok 4)
|
||||
##
|
||||
## [0, 1, 2]
|
||||
|
@ -346,7 +346,7 @@ appendIfOk = \list, result ->
|
|||
appendUnsafe : List a, a -> List a
|
||||
|
||||
## Add a single element to the beginning of a list.
|
||||
## ```
|
||||
## ```roc
|
||||
## List.prepend [1, 2, 3] 0
|
||||
##
|
||||
## [2, 3, 4]
|
||||
|
@ -357,7 +357,7 @@ prepend : List a, a -> List a
|
|||
## If the given [Result] is `Ok`, add it to the beginning of a list.
|
||||
## Otherwise, return the list unmodified.
|
||||
##
|
||||
## ```
|
||||
## ```roc
|
||||
## List.prepend [1, 2, 3] (Ok 0)
|
||||
##
|
||||
## [2, 3, 4]
|
||||
|
@ -387,7 +387,7 @@ reserve : List a, U64 -> List a
|
|||
releaseExcessCapacity : List a -> List a
|
||||
|
||||
## Put two lists together.
|
||||
## ```
|
||||
## ```roc
|
||||
## List.concat [1, 2, 3] [4, 5]
|
||||
##
|
||||
## [0, 1, 2]
|
||||
|
@ -396,7 +396,7 @@ releaseExcessCapacity : List a -> List a
|
|||
concat : List a, List a -> List a
|
||||
|
||||
## Returns the last element in the list, or `ListWasEmpty` if it was empty.
|
||||
## ```
|
||||
## ```roc
|
||||
## expect List.last [1, 2, 3] == Ok 3
|
||||
## expect List.last [] == Err ListWasEmpty
|
||||
## ```
|
||||
|
@ -409,7 +409,7 @@ last = \list ->
|
|||
## A list with a single element in it.
|
||||
##
|
||||
## This is useful in pipelines, like so:
|
||||
## ```
|
||||
## ```roc
|
||||
## websites =
|
||||
## Str.concat domain ".com"
|
||||
## |> List.single
|
||||
|
@ -430,7 +430,7 @@ repeatHelp = \value, count, accum ->
|
|||
accum
|
||||
|
||||
## Returns the list with its elements reversed.
|
||||
## ```
|
||||
## ```roc
|
||||
## expect List.reverse [1, 2, 3] == [3, 2, 1]
|
||||
## ```
|
||||
reverse : List a -> List a
|
||||
|
@ -448,7 +448,7 @@ reverseHelp = \list, left, right ->
|
|||
clone : List a -> List a
|
||||
|
||||
## Join the given lists together into one list.
|
||||
## ```
|
||||
## ```roc
|
||||
## expect List.join [[1], [2, 3], [], [4, 5]] == [1, 2, 3, 4, 5]
|
||||
## expect List.join [[], []] == []
|
||||
## expect List.join [] == []
|
||||
|
@ -471,7 +471,7 @@ contains = \list, needle ->
|
|||
## which updates the `state`. It returns the final `state` at the end.
|
||||
##
|
||||
## You can use it in a pipeline:
|
||||
## ```
|
||||
## ```roc
|
||||
## [2, 4, 8]
|
||||
## |> List.walk 0 Num.add
|
||||
## ```
|
||||
|
@ -490,7 +490,7 @@ contains = \list, needle ->
|
|||
## 6 | 8 | 14
|
||||
##
|
||||
## The following returns -6:
|
||||
## ```
|
||||
## ```roc
|
||||
## [1, 2, 3]
|
||||
## |> List.walk 0 Num.sub
|
||||
## ```
|
||||
|
@ -639,7 +639,7 @@ all = \list, predicate ->
|
|||
|
||||
## Run the given function on each element of a list, and return all the
|
||||
## elements for which the function returned `Bool.true`.
|
||||
## ```
|
||||
## ```roc
|
||||
## List.keepIf [1, 2, 3, 4] (\num -> num > 2)
|
||||
## ```
|
||||
## ## Performance Details
|
||||
|
@ -676,7 +676,7 @@ keepIfHelp = \list, predicate, kept, index, length ->
|
|||
|
||||
## Run the given function on each element of a list, and return all the
|
||||
## elements for which the function returned `Bool.false`.
|
||||
## ```
|
||||
## ```roc
|
||||
## List.dropIf [1, 2, 3, 4] (\num -> num > 2)
|
||||
## ```
|
||||
## ## Performance Details
|
||||
|
@ -689,7 +689,7 @@ dropIf = \list, predicate ->
|
|||
|
||||
## Run the given function on each element of a list, and return the
|
||||
## number of elements for which the function returned `Bool.true`.
|
||||
## ```
|
||||
## ```roc
|
||||
## expect List.countIf [1, -2, -3] Num.isNegative == 2
|
||||
## expect List.countIf [1, 2, 3] (\num -> num > 1 ) == 2
|
||||
## ```
|
||||
|
@ -705,7 +705,7 @@ countIf = \list, predicate ->
|
|||
|
||||
## This works like [List.map], except only the transformed values that are
|
||||
## wrapped in `Ok` are kept. Any that are wrapped in `Err` are dropped.
|
||||
## ```
|
||||
## ```roc
|
||||
## expect List.keepOks ["1", "Two", "23", "Bird"] Str.toI32 == [1, 23]
|
||||
##
|
||||
## expect List.keepOks [["a", "b"], [], ["c", "d", "e"], [] ] List.first == ["a", "c"]
|
||||
|
@ -724,7 +724,7 @@ keepOks = \list, toResult ->
|
|||
|
||||
## This works like [List.map], except only the transformed values that are
|
||||
## wrapped in `Err` are kept. Any that are wrapped in `Ok` are dropped.
|
||||
## ```
|
||||
## ```roc
|
||||
## List.keepErrs [["a", "b"], [], [], ["c", "d", "e"]] List.last
|
||||
##
|
||||
## fn = \str -> if Str.isEmpty str then Err StrWasEmpty else Ok (Str.len str)
|
||||
|
@ -742,7 +742,7 @@ keepErrs = \list, toResult ->
|
|||
|
||||
## Convert each element in the list to something new, by calling a conversion
|
||||
## function on each of them. Then return a new list of the converted values.
|
||||
## ```
|
||||
## ```roc
|
||||
## expect List.map [1, 2, 3] (\num -> num + 1) == [2, 3, 4]
|
||||
##
|
||||
## expect List.map ["", "a", "bc"] Str.isEmpty == [Bool.true, Bool.false, Bool.false]
|
||||
|
@ -755,7 +755,7 @@ map : List a, (a -> b) -> List b
|
|||
##
|
||||
## Some languages have a function named `zip`, which does something similar to
|
||||
## calling [List.map2] passing two lists and `Pair`:
|
||||
## ```
|
||||
## ```roc
|
||||
## zipped = List.map2 ["a", "b", "c"] [1, 2, 3] Pair
|
||||
## ```
|
||||
map2 : List a, List b, (a, b -> c) -> List c
|
||||
|
@ -772,7 +772,7 @@ map4 : List a, List b, List c, List d, (a, b, c, d -> e) -> List e
|
|||
|
||||
## This works like [List.map], except it also passes the index
|
||||
## of the element to the conversion function.
|
||||
## ```
|
||||
## ```roc
|
||||
## expect List.mapWithIndex [10, 20, 30] (\num, index -> num + index) == [10, 21, 32]
|
||||
## ```
|
||||
mapWithIndex : List a, (a, U64 -> b) -> List b
|
||||
|
@ -797,23 +797,23 @@ mapWithIndexHelp = \src, dest, func, index, length ->
|
|||
## Returns a list of all the integers between `start` and `end`.
|
||||
##
|
||||
## To include the `start` and `end` integers themselves, use `At` like so:
|
||||
## ```
|
||||
## ```roc
|
||||
## List.range { start: At 2, end: At 5 } # returns [2, 3, 4, 5]
|
||||
## ```
|
||||
## To exclude them, use `After` and `Before`, like so:
|
||||
## ```
|
||||
## ```roc
|
||||
## List.range { start: After 2, end: Before 5 } # returns [3, 4]
|
||||
## ```
|
||||
## You can have the list end at a certain length rather than a certain integer:
|
||||
## ```
|
||||
## ```roc
|
||||
## List.range { start: At 6, end: Length 4 } # returns [6, 7, 8, 9]
|
||||
## ```
|
||||
## If `step` is specified, each integer increases by that much. (`step: 1` is the default.)
|
||||
## ```
|
||||
## ```roc
|
||||
## List.range { start: After 0, end: Before 9, step: 3 } # returns [3, 6]
|
||||
## ```
|
||||
## List.range will also generate a reversed list if step is negative or end comes before start:
|
||||
## ```
|
||||
## ```roc
|
||||
## List.range { start: At 5, end: At 2 } # returns [5, 4, 3, 2]
|
||||
## ```
|
||||
## All of these options are compatible with the others. For example, you can use `At` or `After`
|
||||
|
@ -966,12 +966,12 @@ first = \list ->
|
|||
Err _ -> Err ListWasEmpty
|
||||
|
||||
## Returns the given number of elements from the beginning of the list.
|
||||
## ```
|
||||
## ```roc
|
||||
## List.takeFirst [1, 2, 3, 4, 5, 6, 7, 8] 4
|
||||
## ```
|
||||
## If there are fewer elements in the list than the requested number,
|
||||
## returns the entire list.
|
||||
## ```
|
||||
## ```roc
|
||||
## List.takeFirst [1, 2] 5
|
||||
## ```
|
||||
## To *remove* elements from the beginning of the list, use `List.takeLast`.
|
||||
|
@ -986,12 +986,12 @@ takeFirst = \list, outputLength ->
|
|||
List.sublist list { start: 0, len: outputLength }
|
||||
|
||||
## Returns the given number of elements from the end of the list.
|
||||
## ```
|
||||
## ```roc
|
||||
## List.takeLast [1, 2, 3, 4, 5, 6, 7, 8] 4
|
||||
## ```
|
||||
## If there are fewer elements in the list than the requested number,
|
||||
## returns the entire list.
|
||||
## ```
|
||||
## ```roc
|
||||
## List.takeLast [1, 2] 5
|
||||
## ```
|
||||
## To *remove* elements from the end of the list, use `List.takeFirst`.
|
||||
|
@ -1132,11 +1132,11 @@ findLastIndex = \list, matches ->
|
|||
## including a total of `len` elements.
|
||||
##
|
||||
## If `start` is outside the bounds of the given list, returns the empty list.
|
||||
## ```
|
||||
## ```roc
|
||||
## List.sublist [1, 2, 3] { start: 4, len: 0 }
|
||||
## ```
|
||||
## If more elements are requested than exist in the list, returns as many as it can.
|
||||
## ```
|
||||
## ```roc
|
||||
## List.sublist [1, 2, 3, 4, 5] { start: 2, len: 10 }
|
||||
## ```
|
||||
## > If you want a sublist which goes all the way to the end of the list, no
|
||||
|
@ -1151,7 +1151,7 @@ sublist = \list, config ->
|
|||
sublistLowlevel : List elem, U64, U64 -> List elem
|
||||
|
||||
## Intersperses `sep` between the elements of `list`
|
||||
## ```
|
||||
## ```roc
|
||||
## List.intersperse [1, 2, 3] 9 # [1, 9, 2, 9, 3]
|
||||
## ```
|
||||
intersperse : List elem, elem -> List elem
|
||||
|
@ -1211,7 +1211,7 @@ split = \elements, userSplitIndex ->
|
|||
|
||||
## Returns the elements before the first occurrence of a delimiter, as well as the
|
||||
## remaining elements after that occurrence. If the delimiter is not found, returns `Err`.
|
||||
## ```
|
||||
## ```roc
|
||||
## List.splitFirst [Foo, Z, Bar, Z, Baz] Z == Ok { before: [Foo], after: [Bar, Z, Baz] }
|
||||
## ```
|
||||
splitFirst : List elem, elem -> Result { before : List elem, after : List elem } [NotFound] where elem implements Eq
|
||||
|
@ -1227,7 +1227,7 @@ splitFirst = \list, delimiter ->
|
|||
|
||||
## Returns the elements before the last occurrence of a delimiter, as well as the
|
||||
## remaining elements after that occurrence. If the delimiter is not found, returns `Err`.
|
||||
## ```
|
||||
## ```roc
|
||||
## List.splitLast [Foo, Z, Bar, Z, Baz] Z == Ok { before: [Foo, Z, Bar], after: [Baz] }
|
||||
## ```
|
||||
splitLast : List elem, elem -> Result { before : List elem, after : List elem } [NotFound] where elem implements Eq
|
||||
|
|
|
@ -388,14 +388,14 @@ Utf8ByteProblem : [
|
|||
Utf8Problem : { byteIndex : U64, problem : Utf8ByteProblem }
|
||||
|
||||
## Returns [Bool.true] if the string is empty, and [Bool.false] otherwise.
|
||||
## ```
|
||||
## ```roc
|
||||
## expect Str.isEmpty "hi!" == Bool.false
|
||||
## expect Str.isEmpty "" == Bool.true
|
||||
## ```
|
||||
isEmpty : Str -> Bool
|
||||
|
||||
## Concatenates two strings together.
|
||||
## ```
|
||||
## ```roc
|
||||
## expect Str.concat "ab" "cd" == "abcd"
|
||||
## expect Str.concat "hello" "" == "hello"
|
||||
## expect Str.concat "" "" == ""
|
||||
|
@ -407,7 +407,7 @@ concat : Str, Str -> Str
|
|||
## This is a performance optimization tool that's like calling [Str.reserve] on an empty string.
|
||||
## It's useful when you plan to build up a string incrementally, for example by calling [Str.concat] on it:
|
||||
##
|
||||
## ```
|
||||
## ```roc
|
||||
## greeting = "Hello and welcome to Roc"
|
||||
## subject = "Awesome Programmer"
|
||||
##
|
||||
|
@ -434,7 +434,7 @@ withCapacity : U64 -> Str
|
|||
## allocating extra capacity up front, which can prevent the need for reallocations and copies.
|
||||
## Consider the following example which does not use [Str.reserve]:
|
||||
##
|
||||
## ```
|
||||
## ```roc
|
||||
## greeting = "Hello and welcome to Roc"
|
||||
## subject = "Awesome Programmer"
|
||||
##
|
||||
|
@ -456,7 +456,7 @@ withCapacity : U64 -> Str
|
|||
##
|
||||
## Here's a modified example which uses [Str.reserve] to eliminate the need for all that reallocation, copying, and deallocation.
|
||||
##
|
||||
## ```
|
||||
## ```roc
|
||||
## helloWorld =
|
||||
## greeting
|
||||
## |> Str.reserve 21
|
||||
|
@ -488,7 +488,7 @@ reserve : Str, U64 -> Str
|
|||
|
||||
## Combines a [List] of strings into a single string, with a separator
|
||||
## string in between each.
|
||||
## ```
|
||||
## ```roc
|
||||
## expect Str.joinWith ["one", "two", "three"] ", " == "one, two, three"
|
||||
## expect Str.joinWith ["1", "2", "3", "4"] "." == "1.2.3.4"
|
||||
## ```
|
||||
|
@ -498,19 +498,19 @@ joinWith : List Str, Str -> Str
|
|||
##
|
||||
## Passing `""` for the separator is not useful;
|
||||
## it returns the original string wrapped in a [List].
|
||||
## ```
|
||||
## ```roc
|
||||
## expect Str.split "1,2,3" "," == ["1","2","3"]
|
||||
## expect Str.split "1,2,3" "" == ["1,2,3"]
|
||||
## ```
|
||||
split : Str, Str -> List Str
|
||||
|
||||
## Repeats a string the given number of times.
|
||||
## ```
|
||||
## ```roc
|
||||
## expect Str.repeat "z" 3 == "zzz"
|
||||
## expect Str.repeat "na" 8 == "nananananananana"
|
||||
## ```
|
||||
## Returns `""` when given `""` for the string or `0` for the count.
|
||||
## ```
|
||||
## ```roc
|
||||
## expect Str.repeat "" 10 == ""
|
||||
## expect Str.repeat "anything" 0 == ""
|
||||
## ```
|
||||
|
@ -519,7 +519,7 @@ repeat : Str, U64 -> Str
|
|||
## Returns a [List] of the string's [U8] UTF-8 [code units](https://unicode.org/glossary/#code_unit).
|
||||
## (To split the string into a [List] of smaller [Str] values instead of [U8] values,
|
||||
## see [Str.split].)
|
||||
## ```
|
||||
## ```roc
|
||||
## expect Str.toUtf8 "Roc" == [82, 111, 99]
|
||||
## expect Str.toUtf8 "鹏" == [233, 185, 143]
|
||||
## expect Str.toUtf8 "சி" == [224, 174, 154, 224, 174, 191]
|
||||
|
@ -530,7 +530,7 @@ toUtf8 : Str -> List U8
|
|||
## Converts a [List] of [U8] UTF-8 [code units](https://unicode.org/glossary/#code_unit) to a string.
|
||||
##
|
||||
## Returns `Err` if the given bytes are invalid UTF-8, and returns `Ok ""` when given `[]`.
|
||||
## ```
|
||||
## ```roc
|
||||
## expect Str.fromUtf8 [82, 111, 99] == Ok "Roc"
|
||||
## expect Str.fromUtf8 [233, 185, 143] == Ok "鹏"
|
||||
## expect Str.fromUtf8 [224, 174, 154, 224, 174, 191] == Ok "சி"
|
||||
|
@ -563,14 +563,14 @@ FromUtf8Result : {
|
|||
fromUtf8Lowlevel : List U8 -> FromUtf8Result
|
||||
|
||||
## Check if the given [Str] starts with a value.
|
||||
## ```
|
||||
## ```roc
|
||||
## expect Str.startsWith "ABC" "A" == Bool.true
|
||||
## expect Str.startsWith "ABC" "X" == Bool.false
|
||||
## ```
|
||||
startsWith : Str, Str -> Bool
|
||||
|
||||
## Check if the given [Str] ends with a value.
|
||||
## ```
|
||||
## ```roc
|
||||
## expect Str.endsWith "ABC" "C" == Bool.true
|
||||
## expect Str.endsWith "ABC" "X" == Bool.false
|
||||
## ```
|
||||
|
@ -578,26 +578,26 @@ endsWith : Str, Str -> Bool
|
|||
|
||||
## Return the [Str] with all whitespace removed from both the beginning
|
||||
## as well as the end.
|
||||
## ```
|
||||
## ```roc
|
||||
## expect Str.trim " Hello \n\n" == "Hello"
|
||||
## ```
|
||||
trim : Str -> Str
|
||||
|
||||
## Return the [Str] with all whitespace removed from the beginning.
|
||||
## ```
|
||||
## ```roc
|
||||
## expect Str.trimStart " Hello \n\n" == "Hello \n\n"
|
||||
## ```
|
||||
trimStart : Str -> Str
|
||||
|
||||
## Return the [Str] with all whitespace removed from the end.
|
||||
## ```
|
||||
## ```roc
|
||||
## expect Str.trimEnd " Hello \n\n" == " Hello"
|
||||
## ```
|
||||
trimEnd : Str -> Str
|
||||
|
||||
## Encode a [Str] to a [Dec]. A [Dec] value is a 128-bit decimal
|
||||
## [fixed-point number](https://en.wikipedia.org/wiki/Fixed-point_arithmetic).
|
||||
## ```
|
||||
## ```roc
|
||||
## expect Str.toDec "10" == Ok 10dec
|
||||
## expect Str.toDec "-0.25" == Ok -0.25dec
|
||||
## expect Str.toDec "not a number" == Err InvalidNumStr
|
||||
|
@ -608,7 +608,7 @@ toDec = \string -> strToNumHelp string
|
|||
## Encode a [Str] to a [F64]. A [F64] value is a 64-bit
|
||||
## [floating-point number](https://en.wikipedia.org/wiki/IEEE_754) and can be
|
||||
## specified with a `f64` suffix.
|
||||
## ```
|
||||
## ```roc
|
||||
## expect Str.toF64 "0.10" == Ok 0.10f64
|
||||
## expect Str.toF64 "not a number" == Err InvalidNumStr
|
||||
## ```
|
||||
|
@ -618,7 +618,7 @@ toF64 = \string -> strToNumHelp string
|
|||
## Encode a [Str] to a [F32].A [F32] value is a 32-bit
|
||||
## [floating-point number](https://en.wikipedia.org/wiki/IEEE_754) and can be
|
||||
## specified with a `f32` suffix.
|
||||
## ```
|
||||
## ```roc
|
||||
## expect Str.toF32 "0.10" == Ok 0.10f32
|
||||
## expect Str.toF32 "not a number" == Err InvalidNumStr
|
||||
## ```
|
||||
|
@ -628,7 +628,7 @@ toF32 = \string -> strToNumHelp string
|
|||
## Encode a [Str] to an unsigned [U128] integer. A [U128] value can hold numbers
|
||||
## from `0` to `340_282_366_920_938_463_463_374_607_431_768_211_455` (over
|
||||
## 340 undecillion). It can be specified with a u128 suffix.
|
||||
## ```
|
||||
## ```roc
|
||||
## expect Str.toU128 "1500" == Ok 1500u128
|
||||
## expect Str.toU128 "0.1" == Err InvalidNumStr
|
||||
## expect Str.toU128 "-1" == Err InvalidNumStr
|
||||
|
@ -641,7 +641,7 @@ toU128 = \string -> strToNumHelp string
|
|||
## from `-170_141_183_460_469_231_731_687_303_715_884_105_728` to
|
||||
## `170_141_183_460_469_231_731_687_303_715_884_105_727`. It can be specified
|
||||
## with a i128 suffix.
|
||||
## ```
|
||||
## ```roc
|
||||
## expect Str.toI128 "1500" == Ok 1500i128
|
||||
## expect Str.toI128 "-1" == Ok -1i128
|
||||
## expect Str.toI128 "0.1" == Err InvalidNumStr
|
||||
|
@ -653,7 +653,7 @@ toI128 = \string -> strToNumHelp string
|
|||
## Encode a [Str] to an unsigned [U64] integer. A [U64] value can hold numbers
|
||||
## from `0` to `18_446_744_073_709_551_615` (over 18 quintillion). It
|
||||
## can be specified with a u64 suffix.
|
||||
## ```
|
||||
## ```roc
|
||||
## expect Str.toU64 "1500" == Ok 1500u64
|
||||
## expect Str.toU64 "0.1" == Err InvalidNumStr
|
||||
## expect Str.toU64 "-1" == Err InvalidNumStr
|
||||
|
@ -665,7 +665,7 @@ toU64 = \string -> strToNumHelp string
|
|||
## Encode a [Str] to a signed [I64] integer. A [I64] value can hold numbers
|
||||
## from `-9_223_372_036_854_775_808` to `9_223_372_036_854_775_807`. It can be
|
||||
## specified with a i64 suffix.
|
||||
## ```
|
||||
## ```roc
|
||||
## expect Str.toI64 "1500" == Ok 1500i64
|
||||
## expect Str.toI64 "-1" == Ok -1i64
|
||||
## expect Str.toI64 "0.1" == Err InvalidNumStr
|
||||
|
@ -677,7 +677,7 @@ toI64 = \string -> strToNumHelp string
|
|||
## Encode a [Str] to an unsigned [U32] integer. A [U32] value can hold numbers
|
||||
## from `0` to `4_294_967_295` (over 4 billion). It can be specified with
|
||||
## a u32 suffix.
|
||||
## ```
|
||||
## ```roc
|
||||
## expect Str.toU32 "1500" == Ok 1500u32
|
||||
## expect Str.toU32 "0.1" == Err InvalidNumStr
|
||||
## expect Str.toU32 "-1" == Err InvalidNumStr
|
||||
|
@ -689,7 +689,7 @@ toU32 = \string -> strToNumHelp string
|
|||
## Encode a [Str] to a signed [I32] integer. A [I32] value can hold numbers
|
||||
## from `-2_147_483_648` to `2_147_483_647`. It can be
|
||||
## specified with a i32 suffix.
|
||||
## ```
|
||||
## ```roc
|
||||
## expect Str.toI32 "1500" == Ok 1500i32
|
||||
## expect Str.toI32 "-1" == Ok -1i32
|
||||
## expect Str.toI32 "0.1" == Err InvalidNumStr
|
||||
|
@ -700,7 +700,7 @@ toI32 = \string -> strToNumHelp string
|
|||
|
||||
## Encode a [Str] to an unsigned [U16] integer. A [U16] value can hold numbers
|
||||
## from `0` to `65_535`. It can be specified with a u16 suffix.
|
||||
## ```
|
||||
## ```roc
|
||||
## expect Str.toU16 "1500" == Ok 1500u16
|
||||
## expect Str.toU16 "0.1" == Err InvalidNumStr
|
||||
## expect Str.toU16 "-1" == Err InvalidNumStr
|
||||
|
@ -712,7 +712,7 @@ toU16 = \string -> strToNumHelp string
|
|||
## Encode a [Str] to a signed [I16] integer. A [I16] value can hold numbers
|
||||
## from `-32_768` to `32_767`. It can be
|
||||
## specified with a i16 suffix.
|
||||
## ```
|
||||
## ```roc
|
||||
## expect Str.toI16 "1500" == Ok 1500i16
|
||||
## expect Str.toI16 "-1" == Ok -1i16
|
||||
## expect Str.toI16 "0.1" == Err InvalidNumStr
|
||||
|
@ -723,7 +723,7 @@ toI16 = \string -> strToNumHelp string
|
|||
|
||||
## Encode a [Str] to an unsigned [U8] integer. A [U8] value can hold numbers
|
||||
## from `0` to `255`. It can be specified with a u8 suffix.
|
||||
## ```
|
||||
## ```roc
|
||||
## expect Str.toU8 "250" == Ok 250u8
|
||||
## expect Str.toU8 "-0.1" == Err InvalidNumStr
|
||||
## expect Str.toU8 "not a number" == Err InvalidNumStr
|
||||
|
@ -735,7 +735,7 @@ toU8 = \string -> strToNumHelp string
|
|||
## Encode a [Str] to a signed [I8] integer. A [I8] value can hold numbers
|
||||
## from `-128` to `127`. It can be
|
||||
## specified with a i8 suffix.
|
||||
## ```
|
||||
## ```roc
|
||||
## expect Str.toI8 "-15" == Ok -15i8
|
||||
## expect Str.toI8 "150.00" == Err InvalidNumStr
|
||||
## expect Str.toI8 "not a number" == Err InvalidNumStr
|
||||
|
@ -747,7 +747,7 @@ toI8 = \string -> strToNumHelp string
|
|||
getUnsafe : Str, U64 -> U8
|
||||
|
||||
## Gives the number of bytes in a [Str] value.
|
||||
## ```
|
||||
## ```roc
|
||||
## expect Str.countUtf8Bytes "Hello World" == 11
|
||||
## ```
|
||||
countUtf8Bytes : Str -> U64
|
||||
|
@ -758,7 +758,7 @@ substringUnsafe : Str, U64, U64 -> Str
|
|||
## Returns the given [Str] with each occurrence of a substring replaced.
|
||||
## If the substring is not found, returns the original string.
|
||||
##
|
||||
## ```
|
||||
## ```roc
|
||||
## expect Str.replaceEach "foo/bar/baz" "/" "_" == "foo_bar_baz"
|
||||
## expect Str.replaceEach "not here" "/" "_" == "not here"
|
||||
## ```
|
||||
|
@ -792,7 +792,7 @@ expect Str.replaceEach "abcdefg" "nothing" "_" == "abcdefg"
|
|||
## Returns the given [Str] with the first occurrence of a substring replaced.
|
||||
## If the substring is not found, returns the original string.
|
||||
##
|
||||
## ```
|
||||
## ```roc
|
||||
## expect Str.replaceFirst "foo/bar/baz" "/" "_" == "foo_bar/baz"
|
||||
## expect Str.replaceFirst "no slashes here" "/" "_" == "no slashes here"
|
||||
## ```
|
||||
|
@ -810,7 +810,7 @@ expect Str.replaceFirst "abcdefg" "nothing" "_" == "abcdefg"
|
|||
## Returns the given [Str] with the last occurrence of a substring replaced.
|
||||
## If the substring is not found, returns the original string.
|
||||
##
|
||||
## ```
|
||||
## ```roc
|
||||
## expect Str.replaceLast "foo/bar/baz" "/" "_" == "foo/bar_baz"
|
||||
## expect Str.replaceLast "no slashes here" "/" "_" == "no slashes here"
|
||||
## ```
|
||||
|
@ -828,7 +828,7 @@ expect Str.replaceLast "abcdefg" "nothing" "_" == "abcdefg"
|
|||
## Returns the given [Str] before the first occurrence of a [delimiter](https://www.computerhope.com/jargon/d/delimite.htm), as well
|
||||
## as the rest of the string after that occurrence.
|
||||
## Returns [Err NotFound] if the delimiter is not found.
|
||||
## ```
|
||||
## ```roc
|
||||
## expect Str.splitFirst "foo/bar/baz" "/" == Ok { before: "foo", after: "bar/baz" }
|
||||
## expect Str.splitFirst "no slashes here" "/" == Err NotFound
|
||||
## ```
|
||||
|
@ -882,7 +882,7 @@ firstMatchHelp = \haystack, needle, index, lastPossible ->
|
|||
## Returns the given [Str] before the last occurrence of a delimiter, as well as
|
||||
## the rest of the string after that occurrence.
|
||||
## Returns [Err NotFound] if the delimiter is not found.
|
||||
## ```
|
||||
## ```roc
|
||||
## expect Str.splitLast "foo/bar/baz" "/" == Ok { before: "foo/bar", after: "baz" }
|
||||
## expect Str.splitLast "no slashes here" "/" == Err NotFound
|
||||
## ```
|
||||
|
@ -974,7 +974,7 @@ matchesAtHelp = \state ->
|
|||
## Walks over the `UTF-8` bytes of the given [Str] and calls a function to update
|
||||
## state for each byte. The index for that byte in the string is provided
|
||||
## to the update function.
|
||||
## ```
|
||||
## ```roc
|
||||
## f : List U8, U8, U64 -> List U8
|
||||
## f = \state, byte, _ -> List.append state byte
|
||||
## expect Str.walkUtf8WithIndex "ABC" [] f == [65, 66, 67]
|
||||
|
@ -996,7 +996,7 @@ walkUtf8WithIndexHelp = \string, state, step, index, length ->
|
|||
## Walks over the `UTF-8` bytes of the given [Str] and calls a function to update
|
||||
## state for each byte.
|
||||
##
|
||||
## ```
|
||||
## ```roc
|
||||
## sumOfUtf8Bytes =
|
||||
## Str.walkUtf8 "Hello, World!" 0 \total, byte ->
|
||||
## total + byte
|
||||
|
@ -1037,14 +1037,14 @@ strToNumHelp = \string ->
|
|||
Err InvalidNumStr
|
||||
|
||||
## Adds a prefix to the given [Str].
|
||||
## ```
|
||||
## ```roc
|
||||
## expect Str.withPrefix "Awesome" "Roc" == "RocAwesome"
|
||||
## ```
|
||||
withPrefix : Str, Str -> Str
|
||||
withPrefix = \str, prefix -> Str.concat prefix str
|
||||
|
||||
## Determines whether or not the first Str contains the second.
|
||||
## ```
|
||||
## ```roc
|
||||
## expect Str.contains "foobarbaz" "bar"
|
||||
## expect !(Str.contains "apple" "orange")
|
||||
## expect Str.contains "anything" ""
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue