Skip to content

Commit

Permalink
V2 (#40)
Browse files Browse the repository at this point in the history
* Prepare v2

* Bump version
  • Loading branch information
gampleman authored Feb 24, 2024
1 parent 77ee5fb commit 3598e8f
Show file tree
Hide file tree
Showing 16 changed files with 35 additions and 778 deletions.
2 changes: 1 addition & 1 deletion docs.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion elm.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "elmcraft/core-extra",
"summary": "Utility functions for an improved experience with elm/core",
"license": "BSD-3-Clause",
"version": "1.0.0",
"version": "2.0.0",
"exposed-modules": [
"Array.Extra",
"Basics.Extra",
Expand Down
58 changes: 1 addition & 57 deletions src/Array/Extra.elm
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ module Array.Extra exposing
, interweave_, andMap, map2, map3, map4, map5, zip, zip3
, resizelRepeat, resizerRepeat, resizelIndexed, resizerIndexed
, mapToList, indexedMapToList
, apply, interweave
)

{-| Convenience functions for working with `Array`
Expand Down Expand Up @@ -46,13 +45,6 @@ module Array.Extra exposing
@docs mapToList, indexedMapToList
# Deprecated functions
These functions are deprecated and **will be removed** in the next major version of this library.
@docs apply, interweave
-}

import Array exposing (Array, append, empty, initialize, length, repeat, slice)
Expand Down Expand Up @@ -144,7 +136,7 @@ pop array =
--> fromList
--> [ "turtles", "on", "turtles", "on", "turtles" ]
To interlace an `Array`, [`interweave`](#interweave).
To interlace an `Array`, [`interweave_`](#interweave_).
-}
intersperse : a -> Array a -> Array a
Expand Down Expand Up @@ -183,26 +175,6 @@ consTry maybeNewHead list =
list


{-| Apply a given `Array` of changes to all elements.
If one `Array` is longer, its extra elements are not used.
import Array exposing (fromList, repeat)
repeat 5 100
|> apply
(fromList
[ \x -> -x, identity, (+) 10 ]
)
--> fromList [ -100, 100, 110 ]
@deprecated in favour of `Array.Extra.andMap` (note the reversed argument order).
-}
apply : Array (a -> b) -> Array a -> Array b
apply changes array =
map2 (\map element -> map element) changes array


{-| Map functions taking multiple arguments over multiple arrays. Each array should be of the same length; extra elements are dropped.
import Array exposing (Array)
Expand Down Expand Up @@ -781,32 +753,6 @@ member needle array =
any (\element -> element == needle) array


{-| Place all elements of a given `Array` between all current elements.
Extra elements of either `Array` are glued to the end without anything in between.
import Array exposing (fromList, repeat)
fromList [ "turtles", "turtles", "turtles" ]
|> interweave (repeat 2 "on")
--> fromList [ "turtles", "on", "turtles", "on", "turtles" ]
fromList [ "turtles", "turtles", "turtles" ]
|> interweave (repeat 5 "on")
--> fromList [ "turtles", "on", "turtles", "on", "turtles", "on", "on", "on" ]
fromList [ "turtles", "turtles", "turtles" ]
|> interweave (repeat 1 "on")
--> fromList [ "turtles", "on", "turtles", "turtles" ]
@deprecated **Beware:** For historical reasons, this function takes it's arguments in the opposite order to [`List.Extra.interweave`](List-Extra#interweave).
As such, this function is deprecated in v1 and will be removed in v2. You should switch to [`interweave_`](#interweave_) which has the correct argument order. We plan to re-introduce `interweave` with the new argument order in a future release, but since the chance of subtle breakage is quite high, we will only do this gradually.
-}
interweave : Array a -> Array a -> Array a
interweave toInterweave array =
interweave_ array toInterweave


{-| Return an array that contains elements from the two provided, in alternate order.
If one array runs out of items, append the items from the remaining array.
Expand All @@ -821,8 +767,6 @@ If one array runs out of items, append the items from the remaining array.
interweave_ (fromList [ "turtles", "turtles", "turtles" ]) (repeat 1 "on")
--> fromList [ "turtles", "on", "turtles", "turtles" ]
See documentation for [`interweave`](#interweave) to find out why the strange name.
-}
interweave_ : Array a -> Array a -> Array a
interweave_ array toInterweave =
Expand Down
218 changes: 0 additions & 218 deletions src/Basics/Extra.elm
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ module Basics.Extra exposing
, safeModBy, safeRemainderBy
, inDegrees, inRadians, inTurns
, flip, curry, uncurry
, swap, atMost, atLeast, fractionalModBy, orderBy, toOrder, toOrderDesc
)

{-| Additional basic functions.
Expand All @@ -30,30 +29,8 @@ module Basics.Extra exposing
@docs flip, curry, uncurry
# Deprecated functions
These functions are deprecated and **will be removed** in the next major version of this library.
@docs swap, atMost, atLeast, fractionalModBy, orderBy, toOrder, toOrderDesc
-}

import Float.Extra
import Order.Extra


{-| Swaps the elements in a pair.
swap ( 1, 2 ) --> ( 2, 1 )
@deprecated in favour of `Tuple.Extra.flip`.
-}
swap : ( a, b ) -> ( b, a )
swap ( a, b ) =
( b, a )


{-| The maximum _safe_ value for an integer, defined as `2^53 - 1`. Anything
larger than that and behaviour becomes mathematically unsound.
Expand Down Expand Up @@ -92,34 +69,6 @@ isSafeInteger number =
minSafeInteger <= number && maxSafeInteger >= number


{-| Defines an upper bound for a variable.
42 |> atMost 0 --> 0
-42 |> atMost 0 --> -42
@deprecated in favour of `min`
-}
atMost : comparable -> comparable -> comparable
atMost =
min


{-| Defines a lower bound for a variable.
-42 |> atLeast 0 --> 0
42 |> atLeast 0 --> 42
@deprecated in favour of `max`
-}
atLeast : comparable -> comparable -> comparable
atLeast =
max


{-| Perform floating-point division (like Elm's `/` operator) that will never
crash the app. If the `y` argument in `safeDivide x y` is zero, we return `Nothing`.
Expand Down Expand Up @@ -209,28 +158,6 @@ safeRemainderBy divisor x =
Just (remainderBy divisor x)


{-| Perform [modular arithmetic](https://en.wikipedia.org/wiki/Modular_arithmetic)
involving floating point numbers.
The sign of the result is the same as the sign of the `modulus`
in `fractionalModBy modulus x`.
fractionalModBy 2.5 5 --> 0
fractionalModBy 2 4.5 == 0.5
fractionalModBy 2 -4.5 == 1.5
fractionalModBy -2 4.5 == -1.5
@deprecated in favour of `Float.Extra.modBy`
-}
fractionalModBy : Float -> Float -> Float
fractionalModBy =
Float.Extra.modBy


{-| Convert standard Elm angles (radians) to degrees.
inDegrees (turns 2) --> 720
Expand Down Expand Up @@ -288,148 +215,3 @@ This combines two arguments into a single pair.
uncurry : (a -> b -> c) -> ( a, b ) -> c
uncurry f ( a, b ) =
f a b


{-| Create an ordering function that can be used to sort
lists by multiple dimensions, by flattening multiple ordering functions into one.
This is equivalent to `ORDER BY` in SQL. The ordering function will order
its inputs based on the order that they appear in the `List (a -> a -> Order)` argument.
type alias Pen =
{ model : String
, tipWidthInMillimeters : Float
}
pens : List Pen
pens =
[ Pen "Pilot Hi-Tec-C Gel" 0.4
, Pen "Morning Glory Pro Mach" 0.38
, Pen "Pilot Hi-Tec-C Coleto" 0.5
]
order : Pen -> Pen -> Order
order =
orderBy [ toOrder .tipWidthInMillimeters, toOrder .model ]
List.sortWith order pens
--> [ Pen "Morning Glory Pro Mach" 0.38
--> , Pen "Pilot Hi-Tec-C Gel" 0.4
--> , Pen "Pilot Hi-Tec-C Coleto" 0.5
--> ]
If our `Pen` type alias above was represented a row in a database table, our `order` function as defined above would be equivalent
to this SQL clause:
ORDER BY tipWidthInMillimeters, model
@deprected in favor of Order.Extra.breakTies
-}
orderBy : List (a -> a -> Order) -> (a -> a -> Order)
orderBy =
Order.Extra.breakTies


{-| Helper for multi-dimensional sort.
Takes a function that extracts a comparable value from a type `a` as a key,
and returns a function `a -> a -> Order`.
This is primarily a helper function for the `orderBy` function above.
{- Simple example: wrapping a function that turns
a custom type into an instance of `comparable`
-}
type Color
= Red
| Yellow
| Green
colorToComparable : Color -> Int
colorToComparable light =
case light of
Red -> 0
Yellow -> 1
Green -> 2
colorToOrder : Color -> Color -> Order
colorToOrder =
toOrder colorToComparable
List.sortWith
colorToOrder
[ Yellow, Yellow, Red, Green, Red ]
--> [ Red, Red, Yellow, Yellow, Green ]
{- More interesting example: using the property accessor
methods on a custom type with `toOrder`; we only need
this function when we want to combine multiple ordering functions into one.
-}
type alias Light =
{ color : Color
, action : String
, timeActivatedSeconds : Float
}
lights : List Light
lights =
[ Light Green "Go" 60
, Light Yellow "Slow down" 5.5
, Light Red "Stop" 60
]
List.sortWith
( orderBy
[ toOrder .timeActivatedSeconds
, toOrder (.color >> colorToComparable)
]
)
lights
--> [ Light Yellow "Slow down" 5.5
--> , Light Red "Stop" 60
--> , Light Green "Go" 60
--> ]
(Note that `List.sortWith colorOrder` above is identical to `List.sortBy colorToComparable`.)
@deprecated in favour of Order.Extra.byField.
-}
toOrder : (a -> comparable) -> (a -> a -> Order)
toOrder selector a b =
Basics.compare (selector a) (selector b)


{-| Same as `toOrder`, with flipped comparisons to enable "sort by descending".
type Color
= Red
| Yellow
| Green
colorToComparable : Color -> Int
colorToComparable light =
case light of
Red -> 0
Yellow -> 1
Green -> 2
colorToOrder : Color -> Color -> Order
colorToOrder =
toOrderDesc colorToComparable
List.sortWith
colorToOrder
[ Yellow, Yellow, Red, Green, Red ]
--> [ Green, Yellow, Yellow, Red, Red ]
@deprecated in favour of `Order.Extra.byField >> Order.Extra.reverse`
-}
toOrderDesc : (a -> comparable) -> (a -> a -> Order)
toOrderDesc selector a b =
Basics.compare (selector b) (selector a)
Loading

0 comments on commit 3598e8f

Please sign in to comment.