diff --git a/src/Array/Extra.elm b/src/Array/Extra.elm index 867fa23..4465d40 100644 --- a/src/Array/Extra.elm +++ b/src/Array/Extra.elm @@ -194,8 +194,8 @@ consTry maybeNewHead list = -} andMap : Array a -> Array (a -> b) -> Array b -andMap = - map2 (|>) +andMap x f = + map2 (|>) x f {-| Apply a function to the elements in the array and collect the result in a List. diff --git a/src/Dict/Extra.elm b/src/Dict/Extra.elm index 317caee..398212d 100644 --- a/src/Dict/Extra.elm +++ b/src/Dict/Extra.elm @@ -60,14 +60,17 @@ groupBy : (a -> comparable) -> List a -> Dict comparable (List a) groupBy keyfn list = List.foldr (\x acc -> - Dict.update (keyfn x) - (\value -> - value - |> Maybe.map ((::) x) - |> Maybe.withDefault [ x ] - |> Just - ) - acc + let + key : comparable + key = + keyfn x + in + case Dict.get key acc of + Nothing -> + Dict.insert key [ x ] acc + + Just found -> + Dict.insert key (x :: found) acc ) Dict.empty list @@ -115,14 +118,12 @@ filterGroupBy keyfn list = (\x acc -> case keyfn x of Just key -> - Dict.update key - (\value -> - value - |> Maybe.map ((::) x) - |> Maybe.withDefault [ x ] - |> Just - ) - acc + case Dict.get key acc of + Nothing -> + Dict.insert key [ x ] acc + + Just found -> + Dict.insert key (x :: found) acc Nothing -> acc @@ -195,15 +196,17 @@ fromListByCombining combine keyfn xs = -} frequencies : List comparable -> Dict comparable Int frequencies list = - list - |> List.foldl - (\el counter -> - Dict.get el counter - |> Maybe.withDefault 0 - |> (\count -> count + 1) - |> (\count -> Dict.insert el count counter) - ) - Dict.empty + List.foldl + (\el counter -> + case Dict.get el counter of + Just count -> + Dict.insert el (count + 1) counter + + Nothing -> + Dict.insert el 1 counter + ) + Dict.empty + list {-| Remove elements which satisfies the predicate. @@ -251,16 +254,12 @@ returns the element to be inserted. -} insertCombining : (v -> v -> v) -> comparable -> v -> Dict comparable v -> Dict comparable v insertCombining combine key value dict = - let - with mbValue = - case mbValue of - Just oldValue -> - Just <| combine oldValue value + case Dict.get key dict of + Nothing -> + Dict.insert key value dict - Nothing -> - Just value - in - Dict.update key with dict + Just oldValue -> + Dict.insert key (combine oldValue value) dict {-| Updates a value if the key is present in the dictionary, leaves the dictionary untouched otherwise. @@ -317,7 +316,12 @@ keepOnly : Set comparable -> Dict comparable v -> Dict comparable v keepOnly set dict = Set.foldl (\k acc -> - Maybe.withDefault acc <| Maybe.map (\v -> Dict.insert k v acc) (Dict.get k dict) + case Dict.get k dict of + Just found -> + Dict.insert k found acc + + Nothing -> + acc ) Dict.empty set diff --git a/src/Float/Extra.elm b/src/Float/Extra.elm index c470515..e8a4608 100644 --- a/src/Float/Extra.elm +++ b/src/Float/Extra.elm @@ -267,20 +267,12 @@ roundToDecimal places value = exp : Float exp = 10.0 ^ toFloat places - - multiplyByExp : Float -> Float - multiplyByExp = - (*) exp - - divByExp : Float -> Float - divByExp v = - v / exp in - value - |> multiplyByExp + ((value * exp) |> round |> toFloat - |> divByExp + ) + / exp diff --git a/src/Order/Extra.elm b/src/Order/Extra.elm index ce9de99..8887583 100644 --- a/src/Order/Extra.elm +++ b/src/Order/Extra.elm @@ -186,8 +186,8 @@ field selected by the given function. -} byField : (a -> comparable) -> a -> a -> Order -byField = - byFieldWith Basics.compare +byField x y = + byFieldWith Basics.compare x y {-| Produces an ordering that orders its elements using the given ordering on the diff --git a/src/Set/Extra.elm b/src/Set/Extra.elm index f88252f..35c0442 100644 --- a/src/Set/Extra.elm +++ b/src/Set/Extra.elm @@ -127,14 +127,14 @@ toggle elem set = -} filterMap : (comparable -> Maybe comparable2) -> Set comparable -> Set comparable2 filterMap f xs = - Set.fromList <| Set.foldr (maybeCons f) [] xs + Set.foldr (maybeInsert f) Set.empty xs -maybeCons : (comparable -> Maybe comparable2) -> comparable -> List comparable2 -> List comparable2 -maybeCons f mx xs = +maybeInsert : (a -> Maybe comparable) -> a -> Set comparable -> Set comparable +maybeInsert f mx xs = case f mx of Just x -> - x :: xs + Set.insert x xs Nothing -> xs diff --git a/src/String/Diacritics.elm b/src/String/Diacritics.elm index 1e58933..81a48a5 100644 --- a/src/String/Diacritics.elm +++ b/src/String/Diacritics.elm @@ -2,6 +2,7 @@ module String.Diacritics exposing (lookupArray, lookupTable, minCode) import Array exposing (Array) import Dict exposing (Dict) +import List.Extra lookupTable : Dict Char String @@ -14,28 +15,27 @@ the strings to replace them with. -} lookupArray : Array String lookupArray = - List.range 0 maxCode - |> List.map - (\i -> - case Dict.get (Char.fromCode i) lookupTable of - Nothing -> - String.fromChar (Char.fromCode i) + Array.initialize (maxCode + 1) + (\i -> + case Dict.get (Char.fromCode i) lookupTable of + Nothing -> + String.fromChar (Char.fromCode i) - Just str -> - str - ) - |> Array.fromList + Just str -> + str + ) {-| The highest Unicode code point of all the diacritics. -} maxCode : Int maxCode = - lookupList - |> List.map Tuple.first - |> List.map Char.toCode - |> List.maximum - |> Maybe.withDefault maxUnicode + case List.Extra.maximumBy (\( c, _ ) -> Char.toCode c) lookupList of + Just ( c, _ ) -> + Char.toCode c + + Nothing -> + maxUnicode {-| The highest Unicode code point, see @@ -50,11 +50,12 @@ maxUnicode = -} minCode : Int minCode = - lookupList - |> List.map Tuple.first - |> List.map Char.toCode - |> List.minimum - |> Maybe.withDefault 0 + case List.Extra.minimumBy (\( c, _ ) -> Char.toCode c) lookupList of + Just ( c, _ ) -> + Char.toCode c + + Nothing -> + 0 lookupList : List ( Char, String ) diff --git a/src/String/Extra.elm b/src/String/Extra.elm index 26617c9..34bebd2 100644 --- a/src/String/Extra.elm +++ b/src/String/Extra.elm @@ -305,13 +305,15 @@ surround wrapper string = unsurround : String -> String -> String unsurround wrapper string = if String.startsWith wrapper string && String.endsWith wrapper string then - let - length = - String.length wrapper - in - string - |> String.dropLeft length - |> String.dropRight length + if String.isEmpty wrapper then + string + + else + let + length = + String.length wrapper + in + String.slice length -length string else string @@ -534,14 +536,24 @@ unindent multilineSting = minLead = lines - |> List.filter (String.any isNotWhitespace) - |> List.map (countLeadingWhitespace 0) + |> List.filterMap + (\s -> + if String.any isNotWhitespace s then + Just (countLeadingWhitespace 0 s) + + else + Nothing + ) |> List.minimum |> Maybe.withDefault 0 in - lines - |> List.map (String.dropLeft minLead) - |> String.join "\n" + if minLead == 0 then + multilineSting + + else + lines + |> List.map (String.dropLeft minLead) + |> String.join "\n" {-| Return the number of occurrences of a substring in another string. diff --git a/src/Tuple/Extra.elm b/src/Tuple/Extra.elm index b84e8c0..fb92b9b 100644 --- a/src/Tuple/Extra.elm +++ b/src/Tuple/Extra.elm @@ -105,8 +105,8 @@ string, join together two strings in a tuple. -} join : appendable -> ( appendable, appendable ) -> appendable -join = - joinBy identity identity +join s ( a, b ) = + a ++ s ++ b {-| Works just like join, but first converts the values of the tuple to strings.