mirror of
https://github.com/sigmasternchen/gleam-community-maths
synced 2025-03-15 07:59:01 +00:00
Merge pull request #22 from gleam-community/list-at-hotfix
`list.at` hotfix
This commit is contained in:
commit
4533abf3ba
4 changed files with 94 additions and 103 deletions
|
@ -1,5 +1,5 @@
|
|||
name = "gleam_community_maths"
|
||||
version = "1.1.0"
|
||||
version = "1.1.1"
|
||||
|
||||
licences = ["Apache-2.0"]
|
||||
description = "A basic maths library"
|
||||
|
@ -7,7 +7,7 @@ repository = { type = "github", user = "gleam-community", repo = "maths" }
|
|||
gleam = ">= 0.32.0"
|
||||
|
||||
[dependencies]
|
||||
gleam_stdlib = "~> 0.34"
|
||||
gleam_stdlib = "~> 0.38"
|
||||
|
||||
[dev-dependencies]
|
||||
gleeunit = "~> 1.0"
|
||||
|
|
|
@ -2,10 +2,10 @@
|
|||
# You typically do not need to edit this file
|
||||
|
||||
packages = [
|
||||
{ name = "gleam_stdlib", version = "0.36.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "C0D14D807FEC6F8A08A7C9EF8DFDE6AE5C10E40E21325B2B29365965D82EB3D4" },
|
||||
{ name = "gleam_stdlib", version = "0.38.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "663CF11861179AF415A625307447775C09404E752FF99A24E2057C835319F1BE" },
|
||||
{ name = "gleeunit", version = "1.1.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "72CDC3D3F719478F26C4E2C5FED3E657AC81EC14A47D2D2DEBB8693CA3220C3B" },
|
||||
]
|
||||
|
||||
[requirements]
|
||||
gleam_stdlib = { version = "~> 0.34" }
|
||||
gleam_stdlib = { version = "~> 0.38" }
|
||||
gleeunit = { version = "~> 1.0" }
|
||||
|
|
|
@ -48,13 +48,13 @@
|
|||
//// * [`standard_deviation`](#standard_deviation)
|
||||
////
|
||||
|
||||
import gleam_community/maths/elementary
|
||||
import gleam_community/maths/piecewise
|
||||
import gleam_community/maths/arithmetics
|
||||
import gleam_community/maths/predicates
|
||||
import gleam_community/maths/conversion
|
||||
import gleam/bool
|
||||
import gleam/list
|
||||
import gleam/pair
|
||||
import gleam_community/maths/arithmetics
|
||||
import gleam_community/maths/conversion
|
||||
import gleam_community/maths/elementary
|
||||
import gleam_community/maths/piecewise
|
||||
import gleam/set
|
||||
import gleam/float
|
||||
import gleam/int
|
||||
|
@ -593,33 +593,31 @@ pub fn mean(arr: List(Float)) -> Result(Float, String) {
|
|||
/// </a>
|
||||
/// </div>
|
||||
///
|
||||
pub fn median(arr: List(Float)) -> Result(Float, String) {
|
||||
case arr {
|
||||
[] ->
|
||||
"Invalid input argument: The list is empty."
|
||||
|> Error
|
||||
_ -> {
|
||||
let count: Int = list.length(arr)
|
||||
let mid: Int = list.length(arr) / 2
|
||||
let sorted: List(Float) = list.sort(arr, float.compare)
|
||||
case predicates.is_odd(count) {
|
||||
// If there is an odd number of elements in the list, then the median
|
||||
// is just the middle value
|
||||
True -> {
|
||||
let assert Ok(val0) = list.at(sorted, mid)
|
||||
val0
|
||||
|> Ok
|
||||
}
|
||||
// If there is an even number of elements in the list, then the median
|
||||
// is the mean of the two middle values
|
||||
False -> {
|
||||
let assert Ok(val0) = list.at(sorted, mid - 1)
|
||||
let assert Ok(val1) = list.at(sorted, mid)
|
||||
[val0, val1]
|
||||
|> mean()
|
||||
}
|
||||
}
|
||||
pub fn median(arr: List(Float)) -> Result(Float, Nil) {
|
||||
use <- bool.guard(list.is_empty(arr), Error(Nil))
|
||||
let length = list.length(arr)
|
||||
let mid = length / 2
|
||||
|
||||
case length % 2 == 0 {
|
||||
True -> do_median(arr, mid, True, 0)
|
||||
False -> do_median(arr, mid, False, 0)
|
||||
}
|
||||
}
|
||||
|
||||
fn do_median(
|
||||
xs: List(Float),
|
||||
mid: Int,
|
||||
mean: Bool,
|
||||
index: Int,
|
||||
) -> Result(Float, Nil) {
|
||||
use <- bool.guard(index > mid, Error(Nil))
|
||||
let mid_less_one = mid - 1
|
||||
|
||||
case xs {
|
||||
[x, ..] if !mean && index == mid -> Ok(x)
|
||||
[x, y, ..] if mean && index == mid_less_one -> Ok({ x +. y } /. 2.0)
|
||||
[_, ..rest] -> do_median(rest, mid, mean, index + 1)
|
||||
[] -> Error(Nil)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -52,11 +52,11 @@
|
|||
//// * [`arg_maximum`](#arg_maximum)
|
||||
////
|
||||
|
||||
import gleam/option
|
||||
import gleam/int
|
||||
import gleam/list
|
||||
import gleam/option
|
||||
import gleam/order
|
||||
import gleam/pair
|
||||
import gleam/int
|
||||
import gleam_community/maths/conversion
|
||||
import gleam_community/maths/elementary
|
||||
|
||||
|
@ -944,17 +944,15 @@ pub fn list_minimum(
|
|||
[] ->
|
||||
"Invalid input argument: The list is empty."
|
||||
|> Error
|
||||
_ -> {
|
||||
let assert Ok(val0) = list.at(arr, 0)
|
||||
arr
|
||||
|> list.fold(val0, fn(acc: a, element: a) {
|
||||
[x, ..rest] ->
|
||||
Ok(
|
||||
list.fold(rest, x, fn(acc: a, element: a) {
|
||||
case compare(element, acc) {
|
||||
order.Lt -> element
|
||||
_ -> acc
|
||||
}
|
||||
})
|
||||
|> Ok
|
||||
}
|
||||
}),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1000,17 +998,15 @@ pub fn list_maximum(
|
|||
[] ->
|
||||
"Invalid input argument: The list is empty."
|
||||
|> Error
|
||||
_ -> {
|
||||
let assert Ok(val0) = list.at(arr, 0)
|
||||
arr
|
||||
|> list.fold(val0, fn(acc: a, element: a) {
|
||||
[x, ..rest] ->
|
||||
Ok(
|
||||
list.fold(rest, x, fn(acc: a, element: a) {
|
||||
case compare(acc, element) {
|
||||
order.Lt -> element
|
||||
_ -> acc
|
||||
}
|
||||
})
|
||||
|> Ok
|
||||
}
|
||||
}),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1202,11 +1198,9 @@ pub fn extrema(
|
|||
[] ->
|
||||
"Invalid input argument: The list is empty."
|
||||
|> Error
|
||||
_ -> {
|
||||
let assert Ok(val_max) = list.at(arr, 0)
|
||||
let assert Ok(val_min) = list.at(arr, 0)
|
||||
arr
|
||||
|> list.fold(#(val_min, val_max), fn(acc: #(a, a), element: a) {
|
||||
[x, ..rest] ->
|
||||
Ok(
|
||||
list.fold(rest, #(x, x), fn(acc: #(a, a), element: a) {
|
||||
let first: a = pair.first(acc)
|
||||
let second: a = pair.second(acc)
|
||||
case compare(element, first), compare(second, element) {
|
||||
|
@ -1215,8 +1209,7 @@ pub fn extrema(
|
|||
_, order.Lt -> #(first, element)
|
||||
_, _ -> #(first, second)
|
||||
}
|
||||
})
|
||||
|> Ok
|
||||
}
|
||||
}),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue