Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Adds sortWith, ascend, descend #58

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions __tests__/Array/sortWith.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { A, F, D, pipe } from '../..'

const xs = [
{
name: 'Diana',
type: 0,
},
{
name: 'Beyonce',
type: 0,
},
{
name: 'Albert',
type: 1,
},
{
name: 'Doris',
type: 1,
},
{
name: 'Alvarez',
type: 0,
},
{
name: 'Bijou',
type: 0,
},
{
name: 'Zac',
type: 0,
},
{
name: 'Poke',
type: 1,
},
]

describe('sort (pipe)', () => {
it('returns correctly sorted array', () => {
const result = pipe(
xs,
A.sortWith([
F.descend(D.getUnsafe('type')),
F.descend(D.getUnsafe('name')),
]),
)
expect(result).toEqual([
{
name: 'Poke',
type: 1,
},
{
name: 'Doris',
type: 1,
},
{
name: 'Albert',
type: 1,
},
{
name: 'Zac',
type: 0,
},
{
name: 'Diana',
type: 0,
},
{
name: 'Bijou',
type: 0,
},
{
name: 'Beyonce',
type: 0,
},
{
name: 'Alvarez',
type: 0,
},
])
})
})
12 changes: 12 additions & 0 deletions __tests__/Function/ascend.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { F, D } from '../..'

describe('ascend', () => {
it('compares value', () => {
expect(F.ascend(D.getUnsafe('a'), { a: 0 }, { a: 1 })).toBe(-1)
expect(F.ascend(D.getUnsafe('a'), { a: 1 }, { a: 0 })).toBe(1)
expect(F.ascend(D.getUnsafe('a'), { a: 2 }, { a: 2 })).toBe(0)
expect(F.ascend(D.getUnsafe('a'), { a: '2' }, { a: '2' })).toBe(0)
expect(F.ascend(D.getUnsafe('a'), { a: 'a' }, { a: 'b' })).toBe(-1)
expect(F.ascend(D.getUnsafe('a'), { a: 'b' }, { a: 'a' })).toBe(1)
})
})
12 changes: 12 additions & 0 deletions __tests__/Function/descend.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { F, D } from '../..'

describe('descend', () => {
it('compares value', () => {
expect(F.descend(D.getUnsafe('a'), { a: 0 }, { a: 1 })).toBe(1)
expect(F.descend(D.getUnsafe('a'), { a: 1 }, { a: 0 })).toBe(-1)
expect(F.descend(D.getUnsafe('a'), { a: 2 }, { a: 2 })).toBe(0)
expect(F.descend(D.getUnsafe('a'), { a: '2' }, { a: '2' })).toBe(0)
expect(F.descend(D.getUnsafe('a'), { a: 'a' }, { a: 'b' })).toBe(1)
expect(F.descend(D.getUnsafe('a'), { a: 'b' }, { a: 'a' })).toBe(-1)
})
})
14 changes: 14 additions & 0 deletions src/Array/Array.res
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,20 @@ let sortBy = (xs, sortFn) =>
a === b ? 0 : a < b ? -1 : 1
})

%comment("Returns a new array, sorted according to the provided functions.")
@gentype
let sortWith = (xs, sortFns) =>
sort(xs, (. a, b) => {
let value = ref(0)
let index = ref(0)
while(value.contents === 0 && index.contents < length(sortFns)) {
let sortFn = Belt.Array.getUnsafe(sortFns, index.contents)
value := sortFn(a, b)
index := index.contents + 1
}
value.contents
})

%comment(
"Splits the given array into sub-arrays in an object, grouped by the result of running each value through the provided function."
)
Expand Down
11 changes: 11 additions & 0 deletions src/Array/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,17 @@ export declare function sortBy<A, B>(
sortFn: (_1: A) => B,
): (xs: Array<A>) => Array<A>

/** Returns a new array, sorted according to the provided functions. */

export declare function sortWith<A>(
sortFns: Array<(_1: A, _2: A) => number>,
): (xs: Array<A>) => Array<A>

export declare function sortWith<A>(
xs: Array<A>,
sortFns: Array<(_1: A, _2: A) => number>,
): Array<A>

/** Splits the given array into sub-arrays in an object, grouped by the result of running each value through the provided function. */

export declare function groupBy<A>(
Expand Down
18 changes: 18 additions & 0 deletions src/Function/Function.res
Original file line number Diff line number Diff line change
Expand Up @@ -288,3 +288,21 @@ let coerce = value => value

@gentype
let andThen = (value, fn) => fn(value)

%comment("Takes comparator fn and compares values ascending")
@genType
let ascend = (fn, a, b) => {
let resultA = fn(a)
let resultB = fn(b)

resultA < resultB ? -1 : resultA > resultB ? 1 : 0
}

%comment("Takes comparator fn and compares values descending")
@genType
let descend = (fn, a, b) => {
let resultA = fn(a)
let resultB = fn(b)

resultA > resultB ? -1 : resultA < resultB ? 1 : 0
}
18 changes: 18 additions & 0 deletions src/Function/Function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,21 @@ export declare function unless<A, B>(
falsyFn: (value: A) => B,
): A | B
export declare function andThen<A, B>(value: A, fn: (value: A) => B): B

export declare function ascend<A, B extends keyof A>(
fn: (value: A) => A[B],
): (a: A, b: A) => number
export declare function ascend<A, B extends keyof A>(
fn: (value: A) => A[B],
a: A,
b: A,
): number

export declare function descend<A, B extends keyof A>(
fn: (value: A) => A[B],
): (a: A, b: A) => number
export declare function descend<A, B extends keyof A>(
fn: (value: A) => A[B],
a: A,
b: A,
): number
38 changes: 38 additions & 0 deletions src/Function/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,41 @@ export declare function coerce<T>(value: any): T

export declare function andThen<A, B>(value: A, fn: (value: A) => B): B
export declare function andThen<A, B>(fn: (value: A) => B): (value: A) => B

/** Takes a value and converts its immutable type to a mutable one. */

export declare function ascend<A, B extends keyof A>(
fn: (value: A) => A[B],
): (a: A, b: A) => number

/** Takes a value and converts its immutable type to a mutable one. */

export declare function ascend<A, B extends keyof A>(
fn: (value: A) => A[B],
a: A,
b: A,
): number

export declare function ascend<A, B extends keyof A>(
a: A,
b: A,
): (fn: (value: A) => A[B]) => number

/** Takes a value and converts its immutable type to a mutable one. */

export declare function descend<A, B extends keyof A>(
fn: (value: A) => A[B],
): (a: A, b: A) => number

/** Takes a value and converts its immutable type to a mutable one. */

export declare function descend<A, B extends keyof A>(
fn: (value: A) => A[B],
a: A,
b: A,
): number

export declare function descend<A, B extends keyof A>(
a: A,
b: A,
): (fn: (value: A) => A[B]) => number