Skip to content

Commit

Permalink
Sorting lists
Browse files Browse the repository at this point in the history
  • Loading branch information
lpil committed Sep 22, 2024
1 parent 7fe3b3e commit 6e1617b
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@

Examples showing how to do many things in Gleam!

- [Algorithms](#algorithms)
- [Cryptography](#cryptography)
- [Data structures](#data-structures)
- [File system](#file-system)
- [Formats](#formats)

## Algorithms

- [Sorting lists](./universal/test/algorithms/sorting_lists.gleam)

## Cryptography

- [Hashing data](./universal/test/cryptography/hashing_data.gleam)
Expand Down
51 changes: 51 additions & 0 deletions universal/test/algorithms/sorting_lists.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//// # Sorting lists
////
//// The Gleam standard library provides functions for sorting lists and
//// ordering core Gleam data types.
////
//// ## Dependencies
////
//// - https://hex.pm/packages/gleam_stdlib

import gleam/float
import gleam/int
import gleam/list
import gleam/order
import gleam/string
import gleeunit/should

pub fn main_test() {
// The `list.sort` function takes an additional argument, a function which is
// used to determine which of any two elements in a list are bigger or
// smaller.
[54, 6, 34, 92, 4]
|> list.sort(int.compare)
|> should.equal([4, 6, 34, 54, 92])

// By convention this function is called `compare` and modules for given data
// types may define this function.
["Tom", "Dick", "Harry"]
|> list.sort(string.compare)
|> should.equal(["Dick", "Harry", "Tom"])

// Lists are sorted from smallest to largest. If you want largest to smallest
// then you can swap the arguments to the compare function.
[3.4, 1.55, 10.4, 8.1]
|> list.sort(fn(a, b) { float.compare(b, a) })
|> should.equal([10.4, 8.1, 3.4, 1.55])

// You can implement your own compare funtion using the `Order` type from the
// `gleam/order` module. Here is a compare function which sorts strings, but
// considers the string `zz-top` to be the smallest for some reason.
["electric six", "the spice girls", "abba", "zz-top", "grlwood"]
|> list.sort(fn(a, b) {
case a, b {
"zz-top", _ -> order.Lt
_, "zz-top" -> order.Gt
a, b -> string.compare(a, b)
}
})
|> should.equal([
"zz-top", "abba", "electric six", "grlwood", "the spice girls",
])
}
1 change: 1 addition & 0 deletions universal/test/cryptography/hashing_data.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//// ## Dependencies
////
//// - https://hex.pm/packages/gleam_crypto
//// - https://hex.pm/packages/gleam_stdlib

import gleam/bit_array
import gleam/crypto
Expand Down
1 change: 1 addition & 0 deletions universal/test/formats/rendering_xml.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//// ## Dependencies
////
//// - https://hex.pm/packages/xmb
//// - https://hex.pm/packages/gleam_stdlib

import gleam/string
import gleam/string_builder
Expand Down

0 comments on commit 6e1617b

Please sign in to comment.