Skip to content

distinctBy를 추가하고 distinct를 변경합니다. #39

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

Merged
merged 3 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
32 changes: 27 additions & 5 deletions src/Garter_Array.mjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 16 additions & 4 deletions src/Garter_Array.res
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,14 @@ let indexBy = (xs, indexFn, ~id) => {

let frequencies = (ar, ~id) => groupBy(ar, Garter_Fn.identity, ~id)->Belt.Map.map(length)

let distinct = (ar, ~id) =>
ar
->reduceU((Belt.Set.make(~id), list{}), ((seen, res), v) =>
let distinctBy = (type a, arr: array<a>, f) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: ar

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

적용했습니다! e9d07ff

module Comparable = Belt.Id.MakeComparableU({
type t = a
let cmp = (a: t, b: t) => Pervasives.compare(f(a), f(b))
})

arr
->reduceU((Belt.Set.make(~id=module(Comparable)), list{}), ((seen, res), v) =>
if seen->Belt.Set.has(v) {
(seen, res)
} else {
Expand All @@ -101,6 +106,11 @@ let distinct = (ar, ~id) =>
->snd
->Belt.List.reverse
->Belt.List.toArray
}

let distinct = arr => {
arr->distinctBy(x => x)
}

let scan = (xs, init, f) => {
let state = makeUninitializedUnsafe(length(xs))
Expand Down Expand Up @@ -235,7 +245,9 @@ module NonEmpty = {

let frequencies = (nxs, ~id) => nxs->frequencies(~id)

let distinct = (nxs, ~id) => nxs->distinct(~id)->fromArrayExn
let distinct = nxs => nxs->distinct->fromArrayExn

let distinctBy = (nxs, f) => nxs->distinctBy(f)->fromArrayExn

let scan = (nxs, init, f) => nxs->scan(init, f)->fromArrayExn

Expand Down
11 changes: 9 additions & 2 deletions src/Garter_Array.resi
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,15 @@ let indexBy: (array<'a>, 'a => 'b, ~id: Belt.Map.id<'b, 'c>) => Belt.Map.t<'b, '
*/
let frequencies: (array<'a>, ~id: Belt.Map.id<'a, 'b>) => Belt.Map.t<'a, int, 'b>

/**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: distinct와 순서를 바꾸면 좋겠습니다

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Garter_Array.res에서는 distinct가 내부적으로 distinctBy를 사용하고 있어 distinctBy가 먼저 선언되어 있습니다. resi에서도 순서를 통일하는 것을 어떤가요?

NonEmpty의 distinct, distinctBy와 순서가 달라서라면 NonEmpty의 순서를 바꿔도 좋을 것 같습니다!

먼저 등장하는 순서를 유지하면서 키에 따라 중복 원소를 제거합니다.
*/
let distinctBy: (array<'a>, 'a => 'b) => array<'a>

/**
먼저 등장하는 순서를 유지하면서 중복 원소를 제거합니다.
*/
let distinct: (array<'a>, ~id: Belt.Set.id<'a, 'b>) => array<'a>
let distinct: array<'a> => array<'a>

/**
`reduce`와 비슷하나 중간 결과를 모두 포함한 `array`를 반환합니다.
Expand Down Expand Up @@ -258,7 +263,9 @@ module NonEmpty: {

let frequencies: (t<'a>, ~id: Belt.Map.id<'a, 'b>) => Belt.Map.t<'a, int, 'b>

let distinct: (t<'a>, ~id: Belt.Set.id<'a, 'b>) => t<'a>
let distinct: t<'a> => t<'a>

let distinctBy: (t<'a>, 'a => 'b) => t<'a>

let scan: (t<'a>, 'b, ('b, 'a) => 'b) => t<'b>

Expand Down
39 changes: 39 additions & 0 deletions test/__tests__/spec/Array_test.mjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions test/__tests__/spec/Array_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,48 @@ zoraBlock("dropWhile", t => {

zoraBlock("keepSome", t => t->testEqual("1", keepSome([Some(1), None, Some(3)]), [1, 3]))

zoraBlock("distinct", t => {
t->testEqual("순서를 유지하면서 중복을 제거한다.", distinct([2, 1, 2]), [2, 1])
})

type a = {
id: int,
name: string,
}

zoraBlock("distinctBy", t => {
t->testEqual(
"순서를 유지하면서 키에 따라 중복을 제거한다.",
distinctBy(
[
{
id: 1,
name: "foo",
},
{
id: 2,
name: "bar",
},
{
id: 3,
name: "foo",
},
],
v => v.name,
),
[
{
id: 1,
name: "foo",
},
{
id: 2,
name: "bar",
},
],
)
})

zoraBlock("intersperse", t => {
t->testEqual("1", intersperse([], 0), [])
t->testEqual("2", intersperse([1], 0), [1])
Expand Down