From 50769b81e6de4285f59bfc93070f9b5458b7592a Mon Sep 17 00:00:00 2001 From: Ayaz Hafiz Date: Wed, 18 May 2022 15:40:02 -0400 Subject: [PATCH] Format other stdlib modules --- compiler/builtins/roc/List.roc | 26 ++++++++------------ compiler/builtins/roc/Result.roc | 42 +++++++++++++++++++++++--------- compiler/fmt/tests/test_fmt.rs | 1 - 3 files changed, 40 insertions(+), 29 deletions(-) diff --git a/compiler/builtins/roc/List.roc b/compiler/builtins/roc/List.roc index 33c3cc415d..242e8d1011 100644 --- a/compiler/builtins/roc/List.roc +++ b/compiler/builtins/roc/List.roc @@ -53,11 +53,10 @@ interface List ] imports [ - Bool.{ Bool } + Bool.{ Bool }, ] ## Types - ## A sequential list of values. ## ## >>> [ 1, 2, 3 ] # a list of numbers @@ -193,10 +192,9 @@ interface List ## * Even when copying is faster, other list operations may still be slightly slower with persistent data structures. For example, even if it were a persistent data structure, [List.map], [List.walk], and [List.keepIf] would all need to traverse every element in the list and build up the result from scratch. These operations are all ## * Roc's compiler optimizes many list operations into in-place mutations behind the scenes, depending on how the list is being used. For example, [List.map], [List.keepIf], and [List.set] can all be optimized to perform in-place mutations. ## * If possible, it is usually best for performance to use large lists in a way where the optimizer can turn them into in-place mutations. If this is not possible, a persistent data structure might be faster - but this is a rare enough scenario that it would not be good for the average Roc program's performance if this were the way [List] worked by default. Instead, you can look outside Roc's standard modules for an implementation of a persistent data structure - likely built using [List] under the hood! - ## Check if the list is empty. ## -## >>> List.isEmpty [ 1, 2, 3 ] +## >>> List.isEmpty [ 1, 2, 3 ] ## ## >>> List.isEmpty [] isEmpty : List a -> Bool @@ -321,7 +319,7 @@ walk : List elem, state, (state, elem -> state) -> state ## Note that in other languages, `walkBackwards` is sometimes called `reduceRight`, ## `fold`, `foldRight`, or `foldr`. -walkBackwards : List elem, state, (state, elem -> state) -> state +walkBackwards : List elem, state, (state, elem -> state) -> state ## Same as [List.walk], except you can stop walking early. ## @@ -406,7 +404,7 @@ keepOks : List before, (before -> Result after *) -> List after ## >>> fn = \str -> if Str.isEmpty str then Err StrWasEmpty else Ok (Str.len str) ## >>> ## >>> List.keepErrs [ "", "a", "bc", "", "d", "ef", "" ] -keepErrs: List before, (before -> Result * after) -> List after +keepErrs : List before, (before -> Result * after) -> List after ## 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. @@ -445,7 +443,7 @@ mapWithIndex : List a, (a, Nat -> b) -> List b ## ## >>> List.range 2 8 range : Int a, Int a -> List (Int a) -sortWith : List a, (a, a -> [ LT, EQ, GT ] ) -> List a +sortWith : List a, (a, a -> [ LT, EQ, GT ]) -> List a ## Sorts a list in ascending order (lowest to highest), using a function which ## specifies a way to represent each element as a number. @@ -541,7 +539,7 @@ drop : List elem, Nat -> List elem ## To replace the element at a given index, instead of dropping it, see [List.set]. dropAt : List elem, Nat -> List elem -min : List (Num a) -> Result (Num a) [ ListWasEmpty ]* +min : List (Num a) -> Result (Num a) [ ListWasEmpty ]* min = \list -> when List.first list is Ok initial -> @@ -550,17 +548,15 @@ min = \list -> Err ListWasEmpty -> Err ListWasEmpty - -minHelp : List (Num a), Num a -> Num a +minHelp : List (Num a), Num a -> Num a minHelp = \list, initial -> List.walk list initial \bestSoFar, current -> if current < bestSoFar then current - else bestSoFar -max : List (Num a) -> Result (Num a) [ ListWasEmpty ]* +max : List (Num a) -> Result (Num a) [ ListWasEmpty ]* max = \list -> when List.first list is Ok initial -> @@ -569,13 +565,11 @@ max = \list -> Err ListWasEmpty -> Err ListWasEmpty - -maxHelp : List (Num a), Num a -> Num a +maxHelp : List (Num a), Num a -> Num a maxHelp = \list, initial -> List.walk list initial \bestSoFar, current -> if current > bestSoFar then current - else bestSoFar @@ -616,4 +610,4 @@ intersperse : List elem, elem -> List elem ## than the given index, # and the `others` list will be all the others. (This ## means if you give an index of 0, the `before` list will be empty and the ## `others` list will have the same elements as the original list.) -split : List elem, Nat -> { before: List elem, others: List elem } +split : List elem, Nat -> { before : List elem, others : List elem } diff --git a/compiler/builtins/roc/Result.roc b/compiler/builtins/roc/Result.roc index 06046299a2..a5daef69d4 100644 --- a/compiler/builtins/roc/Result.roc +++ b/compiler/builtins/roc/Result.roc @@ -12,8 +12,11 @@ Result ok err : [ Ok ok, Err err ] isOk : Result ok err -> Bool isOk = \result -> when result is - Ok _ -> True - Err _ -> False + Ok _ -> + True + + Err _ -> + False ## Return True if the result indicates a failure, else return False ## @@ -21,8 +24,11 @@ isOk = \result -> isErr : Result ok err -> Bool isErr = \result -> when result is - Ok _ -> False - Err _ -> True + Ok _ -> + False + + Err _ -> + True ## If the result is `Ok`, return the value it holds. Otherwise, return ## the given default value. @@ -33,8 +39,11 @@ isErr = \result -> withDefault : Result ok err, ok -> ok withDefault = \result, default -> when result is - Ok value -> value - Err _ -> default + Ok value -> + value + + Err _ -> + default ## If the result is `Ok`, transform the value it holds by running a conversion ## function on it. Then return a new `Ok` holding the transformed value. @@ -50,8 +59,11 @@ withDefault = \result, default -> map : Result a err, (a -> b) -> Result b err map = \result, transform -> when result is - Ok v -> Ok (transform v) - Err e -> Err e + Ok v -> + Ok (transform v) + + Err e -> + Err e ## If the result is `Err`, transform the value it holds by running a conversion ## function on it. Then return a new `Err` holding the transformed value. @@ -64,8 +76,11 @@ map = \result, transform -> mapErr : Result ok a, (a -> b) -> Result ok b mapErr = \result, transform -> when result is - Ok v -> Ok v - Err e -> Err (transform e) + Ok v -> + Ok v + + Err e -> + Err (transform e) ## If the result is `Ok`, transform the entire result by running a conversion ## function on the value the `Ok` holds. Then return that new result. @@ -78,5 +93,8 @@ mapErr = \result, transform -> after : Result a err, (a -> Result b err) -> Result b err after = \result, transform -> when result is - Ok v -> transform v - Err e -> Err e + Ok v -> + transform v + + Err e -> + Err e diff --git a/compiler/fmt/tests/test_fmt.rs b/compiler/fmt/tests/test_fmt.rs index c494f73038..374ad11e30 100644 --- a/compiler/fmt/tests/test_fmt.rs +++ b/compiler/fmt/tests/test_fmt.rs @@ -4653,7 +4653,6 @@ mod test_fmt { } #[test] - #[ignore] /// Test that builtins are formatted correctly /// If this test fails on your diff, it probably means you need to re-format a builtin. /// Try this: