From a1395240b3de160020d3a1d222c12cde19a5368d Mon Sep 17 00:00:00 2001 From: atmcmillon <56739546+atmcmillon@users.noreply.github.com> Date: Mon, 28 Sep 2020 13:33:18 -0500 Subject: [PATCH 01/22] Update shitty_linq_ex.ex Updated module with sum function that sums list values for lists containing only numeric values --- lib/shitty_linq_ex.ex | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/shitty_linq_ex.ex b/lib/shitty_linq_ex.ex index 7e5aa08..f3a4766 100644 --- a/lib/shitty_linq_ex.ex +++ b/lib/shitty_linq_ex.ex @@ -33,3 +33,11 @@ defmodule ShittyLinqEx do [] end end + +def sum([]) do + 0 + end + +def sum([h|t]) do + h + sum(t) + end From 185c28aa7e23a665b78a06f5f4a6493ab8563446 Mon Sep 17 00:00:00 2001 From: atmcmillon <56739546+atmcmillon@users.noreply.github.com> Date: Mon, 28 Sep 2020 13:47:14 -0500 Subject: [PATCH 02/22] Update shitty_linq_ex.ex --- lib/shitty_linq_ex.ex | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/shitty_linq_ex.ex b/lib/shitty_linq_ex.ex index f3a4766..97723c2 100644 --- a/lib/shitty_linq_ex.ex +++ b/lib/shitty_linq_ex.ex @@ -34,6 +34,8 @@ defmodule ShittyLinqEx do end end +#The sum function, finds the sum of an entire list of numeric data + def sum([]) do 0 end From 19507eeef966578ed70e5fb3b7c577b86bc31d20 Mon Sep 17 00:00:00 2001 From: atmcmillon <56739546+atmcmillon@users.noreply.github.com> Date: Mon, 28 Sep 2020 14:09:35 -0500 Subject: [PATCH 03/22] Update shitty_linq_ex.ex --- lib/shitty_linq_ex.ex | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/lib/shitty_linq_ex.ex b/lib/shitty_linq_ex.ex index 97723c2..8d94dd5 100644 --- a/lib/shitty_linq_ex.ex +++ b/lib/shitty_linq_ex.ex @@ -32,14 +32,24 @@ defmodule ShittyLinqEx do defp where_list([], _fun) do [] end -end -#The sum function, finds the sum of an entire list of numeric data + #The sum() function + + @doc """ + Finds the sum of all values in a list with numeric elements. + + ##Examples + + iex> list = [1, 2, 3] + iex> shittyLINQ.sum(list) + 6 + """ -def sum([]) do - 0 - end + def sum([]) do + 0 + end -def sum([h|t]) do - h + sum(t) - end + def sum([h|t]) do + h + sum(t) + end +end From c0d3c8681f2f0ca7130c32f1888361497d92b2cb Mon Sep 17 00:00:00 2001 From: atmcmillon <56739546+atmcmillon@users.noreply.github.com> Date: Mon, 28 Sep 2020 15:55:28 -0500 Subject: [PATCH 04/22] Update shitty_linq_ex.ex Added concat() and append() functions --- lib/shitty_linq_ex.ex | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/lib/shitty_linq_ex.ex b/lib/shitty_linq_ex.ex index 8d94dd5..8d77ea9 100644 --- a/lib/shitty_linq_ex.ex +++ b/lib/shitty_linq_ex.ex @@ -47,9 +47,41 @@ defmodule ShittyLinqEx do def sum([]) do 0 - end + end def sum([h|t]) do h + sum(t) - end + end + +#The concat() function + + @doc """ + Places the elements of two lists into one(concatenate) + + ##Examples + + iex> a = [1, 2, 3] + iex> b = [4, 5, 6] + iex> shittyLINQ.concat(a, b) + [1, 2, 3, 4, 5, 6] + """ + def concat(list1, list2) do + list1 ++ list2 + end + #The concat() function + + @doc """ + Places a new element at the end of an existing list + + ##Examples + + iex> a = [1, 2, 3] + iex> b = shittyLINQ.append(a, 4) + [1, 2, 3, 4] + iex> c = shittyLINQ.append(b, 327) + [1, 2, 3, 4, 327] + """ + def append(list, new) do + list ++ [new] + end end From 564cb3257e785d92abb8c048b61e00b59ff81ddc Mon Sep 17 00:00:00 2001 From: atmcmillon <56739546+atmcmillon@users.noreply.github.com> Date: Mon, 28 Sep 2020 15:56:07 -0500 Subject: [PATCH 05/22] Update shitty_linq_ex.ex --- lib/shitty_linq_ex.ex | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/shitty_linq_ex.ex b/lib/shitty_linq_ex.ex index 8d77ea9..be0fea3 100644 --- a/lib/shitty_linq_ex.ex +++ b/lib/shitty_linq_ex.ex @@ -68,7 +68,8 @@ defmodule ShittyLinqEx do def concat(list1, list2) do list1 ++ list2 end - #The concat() function + + #The append() function @doc """ Places a new element at the end of an existing list From 0998dcf4b10a861c4a9767cf57832669cfdbbe9f Mon Sep 17 00:00:00 2001 From: atmcmillon <56739546+atmcmillon@users.noreply.github.com> Date: Mon, 28 Sep 2020 16:21:27 -0500 Subject: [PATCH 06/22] Update shitty_linq_ex.ex Added the count() function --- lib/shitty_linq_ex.ex | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/shitty_linq_ex.ex b/lib/shitty_linq_ex.ex index be0fea3..f055553 100644 --- a/lib/shitty_linq_ex.ex +++ b/lib/shitty_linq_ex.ex @@ -41,7 +41,7 @@ defmodule ShittyLinqEx do ##Examples iex> list = [1, 2, 3] - iex> shittyLINQ.sum(list) + iex> shittyLinqEx.sum(list) 6 """ @@ -77,12 +77,27 @@ defmodule ShittyLinqEx do ##Examples iex> a = [1, 2, 3] - iex> b = shittyLINQ.append(a, 4) + iex> b = shittyLinqEx.append(a, 4) [1, 2, 3, 4] - iex> c = shittyLINQ.append(b, 327) + iex> c = shittyLinqEx.append(b, 327) [1, 2, 3, 4, 327] """ def append(list, new) do list ++ [new] end + + #The count() function + @doc """ + Finds the number of elements in a list + + ##Examples + + iex> a = [1, 2, 3] + iex> shittyLinqEx.count(a) + 3 + """ + def count(list) do + length(list) + end + end From 500c430fd39dea6119addbeb90bb4a9f4f6cb00c Mon Sep 17 00:00:00 2001 From: atmcmillon <56739546+atmcmillon@users.noreply.github.com> Date: Tue, 29 Sep 2020 11:25:14 -0500 Subject: [PATCH 07/22] Update shitty_linq_ex.ex --- lib/shitty_linq_ex.ex | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/lib/shitty_linq_ex.ex b/lib/shitty_linq_ex.ex index f055553..088af6f 100644 --- a/lib/shitty_linq_ex.ex +++ b/lib/shitty_linq_ex.ex @@ -100,4 +100,20 @@ defmodule ShittyLinqEx do length(list) end + #Average + + def average([]) do + 0 + end + + def average(list) do + sum(list) / count(list) + end + + #Union + + def union([h1|t1], [h2|t2]) do + where(concat([h1|t1], [h2|t2]) + end + end From e021b5b29d8acde56f4f9831c45665e150d6853a Mon Sep 17 00:00:00 2001 From: atmcmillon <56739546+atmcmillon@users.noreply.github.com> Date: Wed, 30 Sep 2020 22:12:00 -0500 Subject: [PATCH 08/22] Add files via upload --- lib/append.exs | 85 ++++++++++++++++++++++++ lib/average.exs | 118 +++++++++++++++++++++++++++++++++ lib/concat.exs | 67 +++++++++++++++++++ lib/count.exs | 99 ++++++++++++++++++++++++++++ lib/intersect.exs | 141 +++++++++++++++++++++++++++++++++++++++ lib/sum.exs | 51 ++++++++++++++ lib/union.exs | 165 ++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 726 insertions(+) create mode 100644 lib/append.exs create mode 100644 lib/average.exs create mode 100644 lib/concat.exs create mode 100644 lib/count.exs create mode 100644 lib/intersect.exs create mode 100644 lib/sum.exs create mode 100644 lib/union.exs diff --git a/lib/append.exs b/lib/append.exs new file mode 100644 index 0000000..9783a82 --- /dev/null +++ b/lib/append.exs @@ -0,0 +1,85 @@ +defmodule ShittyLinqEx do + @moduledoc """ + Documentation for `ShittyLinqEx`. + """ + + @doc """ + Filters a sequence of values based on a predicate. + Where `source` is an enumerable to filter. + Where `predicate` is a function to test each element for a condition. + Returns an enumerable that contains elements from the input sequence that satisfy the condition. + ## Examples + iex> import ShittyLinqEx, only: [where: 2] + iex> where([1, 2, 3], fn x -> rem(x, 2) == 0 end) + [2] + """ + def where(source, predicate) when is_list(source) do + where_list(source, predicate) + end + + defp where_list([head | tail], fun) do + case fun.(head) do + true -> [head | where_list(tail, fun)] + _ -> where_list(tail, fun) + end + end + + defp where_list([], _fun) do + [] + end + + #The sum() function + + @doc """ + Finds the sum of all values in a list with numeric elements. + + ##Examples + + iex> list = [1, 2, 3] + iex> shittyLinqEx.sum(list) + 6 + """ + + def sum([]) do + 0 + end + + def sum([h|t]) do + h + sum(t) + end + + #The concat() function + + @doc """ + Places the elements of two lists into one(concatenate) + + ##Examples + + iex> a = [1, 2, 3] + iex> b = [4, 5, 6] + iex> shittyLINQ.concat(a, b) + [1, 2, 3, 4, 5, 6] + """ + def concat(list1, list2) do + list1 ++ list2 + end + + #The append() function + + @doc """ + Places a new element at the end of an existing list + + ##Examples + + iex> a = [1, 2, 3] + iex> b = shittyLinqEx.append(a, 4) + [1, 2, 3, 4] + iex> c = shittyLinqEx.append(b, 327) + [1, 2, 3, 4, 327] + """ + + def append(list, new) do + list ++ [new] + end + +end \ No newline at end of file diff --git a/lib/average.exs b/lib/average.exs new file mode 100644 index 0000000..5f451c9 --- /dev/null +++ b/lib/average.exs @@ -0,0 +1,118 @@ +defmodule ShittyLinqEx do + @moduledoc """ + Documentation for `ShittyLinqEx`. + """ + + @doc """ + Filters a sequence of values based on a predicate. + Where `source` is an enumerable to filter. + Where `predicate` is a function to test each element for a condition. + Returns an enumerable that contains elements from the input sequence that satisfy the condition. + ## Examples + iex> import ShittyLinqEx, only: [where: 2] + iex> where([1, 2, 3], fn x -> rem(x, 2) == 0 end) + [2] + """ + def where(source, predicate) when is_list(source) do + where_list(source, predicate) + end + + defp where_list([head | tail], fun) do + case fun.(head) do + true -> [head | where_list(tail, fun)] + _ -> where_list(tail, fun) + end + end + + defp where_list([], _fun) do + [] + end + + #The sum() function + + @doc """ + Finds the sum of all values in a list with numeric elements. + + ##Examples + + iex> list = [1, 2, 3] + iex> shittyLinqEx.sum(list) + 6 + """ + + def sum([]) do + 0 + end + + def sum([h|t]) do + h + sum(t) + end + + #The concat() function + + @doc """ + Places the elements of two lists into one(concatenate) + + ##Examples + + iex> a = [1, 2, 3] + iex> b = [4, 5, 6] + iex> shittyLINQ.concat(a, b) + [1, 2, 3, 4, 5, 6] + """ + def concat(list1, list2) do + list1 ++ list2 + end + + #The append() function + + @doc """ + Places a new element at the end of an existing list + + ##Examples + + iex> a = [1, 2, 3] + iex> b = shittyLinqEx.append(a, 4) + [1, 2, 3, 4] + iex> c = shittyLinqEx.append(b, 327) + [1, 2, 3, 4, 327] + """ + + def append(list, new) do + list ++ [new] + end + + #The count() function + @doc """ + Finds the number of elements in a list + + ##Examples + + iex> a = [1, 2, 3] + iex> shittyLinqEx.count(a) + 3 + """ + def count(list) do + length(list) + end + + #The average() function + @doc """ + Finds the arithmetic average of a list of numbers + + ##Examples + + iex> a = [1, 2, 3] + iex> shittyLinqEx.average(a) + 2 + """ + + def average([]) do + 0 + end + + def average(list) do + sum(list) / count(list) + end + +end \ No newline at end of file diff --git a/lib/concat.exs b/lib/concat.exs new file mode 100644 index 0000000..79d2180 --- /dev/null +++ b/lib/concat.exs @@ -0,0 +1,67 @@ +defmodule ShittyLinqEx do + @moduledoc """ + Documentation for `ShittyLinqEx`. + """ + + @doc """ + Filters a sequence of values based on a predicate. + Where `source` is an enumerable to filter. + Where `predicate` is a function to test each element for a condition. + Returns an enumerable that contains elements from the input sequence that satisfy the condition. + ## Examples + iex> import ShittyLinqEx, only: [where: 2] + iex> where([1, 2, 3], fn x -> rem(x, 2) == 0 end) + [2] + """ + def where(source, predicate) when is_list(source) do + where_list(source, predicate) + end + + defp where_list([head | tail], fun) do + case fun.(head) do + true -> [head | where_list(tail, fun)] + _ -> where_list(tail, fun) + end + end + + defp where_list([], _fun) do + [] + end + + #The sum() function + + @doc """ + Finds the sum of all values in a list with numeric elements. + + ##Examples + + iex> list = [1, 2, 3] + iex> shittyLinqEx.sum(list) + 6 + """ + + def sum([]) do + 0 + end + + def sum([h|t]) do + h + sum(t) + end + + #The concat() function + + @doc """ + Places the elements of two lists into one(concatenate) + + ##Examples + + iex> a = [1, 2, 3] + iex> b = [4, 5, 6] + iex> shittyLINQ.concat(a, b) + [1, 2, 3, 4, 5, 6] + """ + def concat(list1, list2) do + list1 ++ list2 + end + +end \ No newline at end of file diff --git a/lib/count.exs b/lib/count.exs new file mode 100644 index 0000000..61d6acb --- /dev/null +++ b/lib/count.exs @@ -0,0 +1,99 @@ +defmodule ShittyLinqEx do + @moduledoc """ + Documentation for `ShittyLinqEx`. + """ + + @doc """ + Filters a sequence of values based on a predicate. + Where `source` is an enumerable to filter. + Where `predicate` is a function to test each element for a condition. + Returns an enumerable that contains elements from the input sequence that satisfy the condition. + ## Examples + iex> import ShittyLinqEx, only: [where: 2] + iex> where([1, 2, 3], fn x -> rem(x, 2) == 0 end) + [2] + """ + def where(source, predicate) when is_list(source) do + where_list(source, predicate) + end + + defp where_list([head | tail], fun) do + case fun.(head) do + true -> [head | where_list(tail, fun)] + _ -> where_list(tail, fun) + end + end + + defp where_list([], _fun) do + [] + end + + #The sum() function + + @doc """ + Finds the sum of all values in a list with numeric elements. + + ##Examples + + iex> list = [1, 2, 3] + iex> shittyLinqEx.sum(list) + 6 + """ + + def sum([]) do + 0 + end + + def sum([h|t]) do + h + sum(t) + end + + #The concat() function + + @doc """ + Places the elements of two lists into one(concatenate) + + ##Examples + + iex> a = [1, 2, 3] + iex> b = [4, 5, 6] + iex> shittyLINQ.concat(a, b) + [1, 2, 3, 4, 5, 6] + """ + def concat(list1, list2) do + list1 ++ list2 + end + + #The append() function + + @doc """ + Places a new element at the end of an existing list + + ##Examples + + iex> a = [1, 2, 3] + iex> b = shittyLinqEx.append(a, 4) + [1, 2, 3, 4] + iex> c = shittyLinqEx.append(b, 327) + [1, 2, 3, 4, 327] + """ + + def append(list, new) do + list ++ [new] + end + + #The count() function + @doc """ + Finds the number of elements in a list + + ##Examples + + iex> a = [1, 2, 3] + iex> shittyLinqEx.count(a) + 3 + """ + def count(list) do + length(list) + end + +end \ No newline at end of file diff --git a/lib/intersect.exs b/lib/intersect.exs new file mode 100644 index 0000000..85c5d50 --- /dev/null +++ b/lib/intersect.exs @@ -0,0 +1,141 @@ +defmodule ShittyLinqEx do + @moduledoc """ + Documentation for `ShittyLinqEx`. + """ + + @doc """ + Filters a sequence of values based on a predicate. + Where `source` is an enumerable to filter. + Where `predicate` is a function to test each element for a condition. + Returns an enumerable that contains elements from the input sequence that satisfy the condition. + ## Examples + iex> import ShittyLinqEx, only: [where: 2] + iex> where([1, 2, 3], fn x -> rem(x, 2) == 0 end) + [2] + """ + def where(source, predicate) when is_list(source) do + where_list(source, predicate) + end + + defp where_list([head | tail], fun) do + case fun.(head) do + true -> [head | where_list(tail, fun)] + _ -> where_list(tail, fun) + end + end + + defp where_list([], _fun) do + [] + end + + #The sum() function + + @doc """ + Finds the sum of all values in a list with numeric elements. + + ##Examples + + iex> list = [1, 2, 3] + iex> shittyLinqEx.sum(list) + 6 + """ + + def sum([]) do + 0 + end + + def sum([h|t]) do + h + sum(t) + end + + #The concat() function + + @doc """ + Places the elements of two lists into one(concatenate) + + ##Examples + + iex> a = [1, 2, 3] + iex> b = [4, 5, 6] + iex> shittyLINQ.concat(a, b) + [1, 2, 3, 4, 5, 6] + """ + def concat(list1, list2) do + list1 ++ list2 + end + + #The append() function + + @doc """ + Places a new element at the end of an existing list + + ##Examples + + iex> a = [1, 2, 3] + iex> b = shittyLinqEx.append(a, 4) + [1, 2, 3, 4] + iex> c = shittyLinqEx.append(b, 327) + [1, 2, 3, 4, 327] + """ + + def append(list, new) do + list ++ [new] + end + + #The count() function + @doc """ + Finds the number of elements in a list + + ##Examples + + iex> a = [1, 2, 3] + iex> shittyLinqEx.count(a) + 3 + """ + def count(list) do + length(list) + end + + #The average() function + @doc """ + Finds the arithmetic average of a list of numbers + + ##Examples + + iex> a = [1, 2, 3] + iex> shittyLinqEx.average(a) + 2 + """ + + def average([]) do + 0 + end + + def average(list) do + sum(list) / count(list) + end + + #The intersect() function + + @doc """ + Finds the intersection of 2 lists(where they have elements in common) + + ##Examples: + + iex> a = [1, 2, 3, 4] + iex> b = [2, 3, 4, 5] + iex> c = ShittyLinqEx.intersect(a, b) + [2, 3, 4] + + iex> a = [6, 42, 2] + iex> b = [73, 37, 1] + iex> c = ShittyLinqEx.intersect(a, b) + [] + """ + + def intersect(list1, list2) do + list3 = list1 -- list2 + list4 = list1 -- list3 + end + +end \ No newline at end of file diff --git a/lib/sum.exs b/lib/sum.exs new file mode 100644 index 0000000..636c2f0 --- /dev/null +++ b/lib/sum.exs @@ -0,0 +1,51 @@ +defmodule ShittyLinqEx do + @moduledoc """ + Documentation for `ShittyLinqEx`. + """ + + @doc """ + Filters a sequence of values based on a predicate. + Where `source` is an enumerable to filter. + Where `predicate` is a function to test each element for a condition. + Returns an enumerable that contains elements from the input sequence that satisfy the condition. + ## Examples + iex> import ShittyLinqEx, only: [where: 2] + iex> where([1, 2, 3], fn x -> rem(x, 2) == 0 end) + [2] + """ + def where(source, predicate) when is_list(source) do + where_list(source, predicate) + end + + defp where_list([head | tail], fun) do + case fun.(head) do + true -> [head | where_list(tail, fun)] + _ -> where_list(tail, fun) + end + end + + defp where_list([], _fun) do + [] + end + + #The sum() function + + @doc """ + Finds the sum of all values in a list with numeric elements. + + ##Examples + + iex> list = [1, 2, 3] + iex> shittyLinqEx.sum(list) + 6 + """ + + def sum([]) do + 0 + end + + def sum([h|t]) do + h + sum(t) + end + +end \ No newline at end of file diff --git a/lib/union.exs b/lib/union.exs new file mode 100644 index 0000000..5e677c9 --- /dev/null +++ b/lib/union.exs @@ -0,0 +1,165 @@ +defmodule ShittyLinqEx do + @moduledoc """ + Documentation for `ShittyLinqEx`. + """ + + @doc """ + Filters a sequence of values based on a predicate. + Where `source` is an enumerable to filter. + Where `predicate` is a function to test each element for a condition. + Returns an enumerable that contains elements from the input sequence that satisfy the condition. + ## Examples + iex> import ShittyLinqEx, only: [where: 2] + iex> where([1, 2, 3], fn x -> rem(x, 2) == 0 end) + [2] + """ + def where(source, predicate) when is_list(source) do + where_list(source, predicate) + end + + defp where_list([head | tail], fun) do + case fun.(head) do + true -> [head | where_list(tail, fun)] + _ -> where_list(tail, fun) + end + end + + defp where_list([], _fun) do + [] + end + + #The sum() function + + @doc """ + Finds the sum of all values in a list with numeric elements. + + ##Examples + + iex> list = [1, 2, 3] + iex> shittyLinqEx.sum(list) + 6 + """ + + def sum([]) do + 0 + end + + def sum([h|t]) do + h + sum(t) + end + + #The concat() function + + @doc """ + Places the elements of two lists into one(concatenate) + + ##Examples + + iex> a = [1, 2, 3] + iex> b = [4, 5, 6] + iex> shittyLINQ.concat(a, b) + [1, 2, 3, 4, 5, 6] + """ + def concat(list1, list2) do + list1 ++ list2 + end + + #The append() function + + @doc """ + Places a new element at the end of an existing list + + ##Examples + + iex> a = [1, 2, 3] + iex> b = shittyLinqEx.append(a, 4) + [1, 2, 3, 4] + iex> c = shittyLinqEx.append(b, 327) + [1, 2, 3, 4, 327] + """ + + def append(list, new) do + list ++ [new] + end + + #The count() function + @doc """ + Finds the number of elements in a list + + ##Examples + + iex> a = [1, 2, 3] + iex> shittyLinqEx.count(a) + 3 + """ + def count(list) do + length(list) + end + + #The average() function + @doc """ + Finds the arithmetic average of a list of numbers + + ##Examples + + iex> a = [1, 2, 3] + iex> shittyLinqEx.average(a) + 2 + """ + + def average([]) do + 0 + end + + def average(list) do + sum(list) / count(list) + end + + #The intersect() function + + @doc """ + Finds the intersection of 2 lists(where they have elements in common) + + ##Examples: + + iex> a = [1, 2, 3, 4] + iex> b = [2, 3, 4, 5] + iex> c = ShittyLinqEx.intersect(a, b) + [2, 3, 4] + + iex> a = [6, 42, 2] + iex> b = [73, 37, 1] + iex> c = ShittyLinqEx.intersect(a, b) + [] + """ + + def intersect(list1, list2) do + list3 = list1 -- list2 + list4 = list1 -- list3 + end + + #The union() function + + @doc """ + + Finds the union of two sets(combines both sets and excludes duplicates) + + ##Examples + + iex> a = [1, 37, 73] + iex> b = [1, 37, 97, 79] + iex> ShittyLinqEx.union(a, b) + [1, 37, 73, 97, 79] + """ + + def union(a, b) do + c = a -- b + d = b -- a + e = c ++ d + f = a -- c + g = f ++ e + end + + #The order_by() function + +end \ No newline at end of file From b665644acfe4911260ef13c6ac134da95ba534a8 Mon Sep 17 00:00:00 2001 From: atmcmillon <56739546+atmcmillon@users.noreply.github.com> Date: Wed, 30 Sep 2020 22:45:38 -0500 Subject: [PATCH 09/22] Delete append.exs --- lib/append.exs | 85 -------------------------------------------------- 1 file changed, 85 deletions(-) delete mode 100644 lib/append.exs diff --git a/lib/append.exs b/lib/append.exs deleted file mode 100644 index 9783a82..0000000 --- a/lib/append.exs +++ /dev/null @@ -1,85 +0,0 @@ -defmodule ShittyLinqEx do - @moduledoc """ - Documentation for `ShittyLinqEx`. - """ - - @doc """ - Filters a sequence of values based on a predicate. - Where `source` is an enumerable to filter. - Where `predicate` is a function to test each element for a condition. - Returns an enumerable that contains elements from the input sequence that satisfy the condition. - ## Examples - iex> import ShittyLinqEx, only: [where: 2] - iex> where([1, 2, 3], fn x -> rem(x, 2) == 0 end) - [2] - """ - def where(source, predicate) when is_list(source) do - where_list(source, predicate) - end - - defp where_list([head | tail], fun) do - case fun.(head) do - true -> [head | where_list(tail, fun)] - _ -> where_list(tail, fun) - end - end - - defp where_list([], _fun) do - [] - end - - #The sum() function - - @doc """ - Finds the sum of all values in a list with numeric elements. - - ##Examples - - iex> list = [1, 2, 3] - iex> shittyLinqEx.sum(list) - 6 - """ - - def sum([]) do - 0 - end - - def sum([h|t]) do - h + sum(t) - end - - #The concat() function - - @doc """ - Places the elements of two lists into one(concatenate) - - ##Examples - - iex> a = [1, 2, 3] - iex> b = [4, 5, 6] - iex> shittyLINQ.concat(a, b) - [1, 2, 3, 4, 5, 6] - """ - def concat(list1, list2) do - list1 ++ list2 - end - - #The append() function - - @doc """ - Places a new element at the end of an existing list - - ##Examples - - iex> a = [1, 2, 3] - iex> b = shittyLinqEx.append(a, 4) - [1, 2, 3, 4] - iex> c = shittyLinqEx.append(b, 327) - [1, 2, 3, 4, 327] - """ - - def append(list, new) do - list ++ [new] - end - -end \ No newline at end of file From 1663f330b52a3ebcdccae1031ac40b64dde0bafc Mon Sep 17 00:00:00 2001 From: atmcmillon <56739546+atmcmillon@users.noreply.github.com> Date: Wed, 30 Sep 2020 22:45:48 -0500 Subject: [PATCH 10/22] Delete average.exs --- lib/average.exs | 118 ------------------------------------------------ 1 file changed, 118 deletions(-) delete mode 100644 lib/average.exs diff --git a/lib/average.exs b/lib/average.exs deleted file mode 100644 index 5f451c9..0000000 --- a/lib/average.exs +++ /dev/null @@ -1,118 +0,0 @@ -defmodule ShittyLinqEx do - @moduledoc """ - Documentation for `ShittyLinqEx`. - """ - - @doc """ - Filters a sequence of values based on a predicate. - Where `source` is an enumerable to filter. - Where `predicate` is a function to test each element for a condition. - Returns an enumerable that contains elements from the input sequence that satisfy the condition. - ## Examples - iex> import ShittyLinqEx, only: [where: 2] - iex> where([1, 2, 3], fn x -> rem(x, 2) == 0 end) - [2] - """ - def where(source, predicate) when is_list(source) do - where_list(source, predicate) - end - - defp where_list([head | tail], fun) do - case fun.(head) do - true -> [head | where_list(tail, fun)] - _ -> where_list(tail, fun) - end - end - - defp where_list([], _fun) do - [] - end - - #The sum() function - - @doc """ - Finds the sum of all values in a list with numeric elements. - - ##Examples - - iex> list = [1, 2, 3] - iex> shittyLinqEx.sum(list) - 6 - """ - - def sum([]) do - 0 - end - - def sum([h|t]) do - h + sum(t) - end - - #The concat() function - - @doc """ - Places the elements of two lists into one(concatenate) - - ##Examples - - iex> a = [1, 2, 3] - iex> b = [4, 5, 6] - iex> shittyLINQ.concat(a, b) - [1, 2, 3, 4, 5, 6] - """ - def concat(list1, list2) do - list1 ++ list2 - end - - #The append() function - - @doc """ - Places a new element at the end of an existing list - - ##Examples - - iex> a = [1, 2, 3] - iex> b = shittyLinqEx.append(a, 4) - [1, 2, 3, 4] - iex> c = shittyLinqEx.append(b, 327) - [1, 2, 3, 4, 327] - """ - - def append(list, new) do - list ++ [new] - end - - #The count() function - @doc """ - Finds the number of elements in a list - - ##Examples - - iex> a = [1, 2, 3] - iex> shittyLinqEx.count(a) - 3 - """ - def count(list) do - length(list) - end - - #The average() function - @doc """ - Finds the arithmetic average of a list of numbers - - ##Examples - - iex> a = [1, 2, 3] - iex> shittyLinqEx.average(a) - 2 - """ - - def average([]) do - 0 - end - - def average(list) do - sum(list) / count(list) - end - -end \ No newline at end of file From f3a17d4dec17a6a91c394dc2d9bb5301bcf246ac Mon Sep 17 00:00:00 2001 From: atmcmillon <56739546+atmcmillon@users.noreply.github.com> Date: Wed, 30 Sep 2020 22:45:57 -0500 Subject: [PATCH 11/22] Delete concat.exs --- lib/concat.exs | 67 -------------------------------------------------- 1 file changed, 67 deletions(-) delete mode 100644 lib/concat.exs diff --git a/lib/concat.exs b/lib/concat.exs deleted file mode 100644 index 79d2180..0000000 --- a/lib/concat.exs +++ /dev/null @@ -1,67 +0,0 @@ -defmodule ShittyLinqEx do - @moduledoc """ - Documentation for `ShittyLinqEx`. - """ - - @doc """ - Filters a sequence of values based on a predicate. - Where `source` is an enumerable to filter. - Where `predicate` is a function to test each element for a condition. - Returns an enumerable that contains elements from the input sequence that satisfy the condition. - ## Examples - iex> import ShittyLinqEx, only: [where: 2] - iex> where([1, 2, 3], fn x -> rem(x, 2) == 0 end) - [2] - """ - def where(source, predicate) when is_list(source) do - where_list(source, predicate) - end - - defp where_list([head | tail], fun) do - case fun.(head) do - true -> [head | where_list(tail, fun)] - _ -> where_list(tail, fun) - end - end - - defp where_list([], _fun) do - [] - end - - #The sum() function - - @doc """ - Finds the sum of all values in a list with numeric elements. - - ##Examples - - iex> list = [1, 2, 3] - iex> shittyLinqEx.sum(list) - 6 - """ - - def sum([]) do - 0 - end - - def sum([h|t]) do - h + sum(t) - end - - #The concat() function - - @doc """ - Places the elements of two lists into one(concatenate) - - ##Examples - - iex> a = [1, 2, 3] - iex> b = [4, 5, 6] - iex> shittyLINQ.concat(a, b) - [1, 2, 3, 4, 5, 6] - """ - def concat(list1, list2) do - list1 ++ list2 - end - -end \ No newline at end of file From e42ba6df3b687b0d0727ca158c0ee24c86f50bd4 Mon Sep 17 00:00:00 2001 From: atmcmillon <56739546+atmcmillon@users.noreply.github.com> Date: Wed, 30 Sep 2020 22:46:10 -0500 Subject: [PATCH 12/22] Delete count.exs --- lib/count.exs | 99 --------------------------------------------------- 1 file changed, 99 deletions(-) delete mode 100644 lib/count.exs diff --git a/lib/count.exs b/lib/count.exs deleted file mode 100644 index 61d6acb..0000000 --- a/lib/count.exs +++ /dev/null @@ -1,99 +0,0 @@ -defmodule ShittyLinqEx do - @moduledoc """ - Documentation for `ShittyLinqEx`. - """ - - @doc """ - Filters a sequence of values based on a predicate. - Where `source` is an enumerable to filter. - Where `predicate` is a function to test each element for a condition. - Returns an enumerable that contains elements from the input sequence that satisfy the condition. - ## Examples - iex> import ShittyLinqEx, only: [where: 2] - iex> where([1, 2, 3], fn x -> rem(x, 2) == 0 end) - [2] - """ - def where(source, predicate) when is_list(source) do - where_list(source, predicate) - end - - defp where_list([head | tail], fun) do - case fun.(head) do - true -> [head | where_list(tail, fun)] - _ -> where_list(tail, fun) - end - end - - defp where_list([], _fun) do - [] - end - - #The sum() function - - @doc """ - Finds the sum of all values in a list with numeric elements. - - ##Examples - - iex> list = [1, 2, 3] - iex> shittyLinqEx.sum(list) - 6 - """ - - def sum([]) do - 0 - end - - def sum([h|t]) do - h + sum(t) - end - - #The concat() function - - @doc """ - Places the elements of two lists into one(concatenate) - - ##Examples - - iex> a = [1, 2, 3] - iex> b = [4, 5, 6] - iex> shittyLINQ.concat(a, b) - [1, 2, 3, 4, 5, 6] - """ - def concat(list1, list2) do - list1 ++ list2 - end - - #The append() function - - @doc """ - Places a new element at the end of an existing list - - ##Examples - - iex> a = [1, 2, 3] - iex> b = shittyLinqEx.append(a, 4) - [1, 2, 3, 4] - iex> c = shittyLinqEx.append(b, 327) - [1, 2, 3, 4, 327] - """ - - def append(list, new) do - list ++ [new] - end - - #The count() function - @doc """ - Finds the number of elements in a list - - ##Examples - - iex> a = [1, 2, 3] - iex> shittyLinqEx.count(a) - 3 - """ - def count(list) do - length(list) - end - -end \ No newline at end of file From c7b5b4a43a3a918f2b5b33422b512e643bb50a81 Mon Sep 17 00:00:00 2001 From: atmcmillon <56739546+atmcmillon@users.noreply.github.com> Date: Wed, 30 Sep 2020 22:46:19 -0500 Subject: [PATCH 13/22] Delete intersect.exs --- lib/intersect.exs | 141 ---------------------------------------------- 1 file changed, 141 deletions(-) delete mode 100644 lib/intersect.exs diff --git a/lib/intersect.exs b/lib/intersect.exs deleted file mode 100644 index 85c5d50..0000000 --- a/lib/intersect.exs +++ /dev/null @@ -1,141 +0,0 @@ -defmodule ShittyLinqEx do - @moduledoc """ - Documentation for `ShittyLinqEx`. - """ - - @doc """ - Filters a sequence of values based on a predicate. - Where `source` is an enumerable to filter. - Where `predicate` is a function to test each element for a condition. - Returns an enumerable that contains elements from the input sequence that satisfy the condition. - ## Examples - iex> import ShittyLinqEx, only: [where: 2] - iex> where([1, 2, 3], fn x -> rem(x, 2) == 0 end) - [2] - """ - def where(source, predicate) when is_list(source) do - where_list(source, predicate) - end - - defp where_list([head | tail], fun) do - case fun.(head) do - true -> [head | where_list(tail, fun)] - _ -> where_list(tail, fun) - end - end - - defp where_list([], _fun) do - [] - end - - #The sum() function - - @doc """ - Finds the sum of all values in a list with numeric elements. - - ##Examples - - iex> list = [1, 2, 3] - iex> shittyLinqEx.sum(list) - 6 - """ - - def sum([]) do - 0 - end - - def sum([h|t]) do - h + sum(t) - end - - #The concat() function - - @doc """ - Places the elements of two lists into one(concatenate) - - ##Examples - - iex> a = [1, 2, 3] - iex> b = [4, 5, 6] - iex> shittyLINQ.concat(a, b) - [1, 2, 3, 4, 5, 6] - """ - def concat(list1, list2) do - list1 ++ list2 - end - - #The append() function - - @doc """ - Places a new element at the end of an existing list - - ##Examples - - iex> a = [1, 2, 3] - iex> b = shittyLinqEx.append(a, 4) - [1, 2, 3, 4] - iex> c = shittyLinqEx.append(b, 327) - [1, 2, 3, 4, 327] - """ - - def append(list, new) do - list ++ [new] - end - - #The count() function - @doc """ - Finds the number of elements in a list - - ##Examples - - iex> a = [1, 2, 3] - iex> shittyLinqEx.count(a) - 3 - """ - def count(list) do - length(list) - end - - #The average() function - @doc """ - Finds the arithmetic average of a list of numbers - - ##Examples - - iex> a = [1, 2, 3] - iex> shittyLinqEx.average(a) - 2 - """ - - def average([]) do - 0 - end - - def average(list) do - sum(list) / count(list) - end - - #The intersect() function - - @doc """ - Finds the intersection of 2 lists(where they have elements in common) - - ##Examples: - - iex> a = [1, 2, 3, 4] - iex> b = [2, 3, 4, 5] - iex> c = ShittyLinqEx.intersect(a, b) - [2, 3, 4] - - iex> a = [6, 42, 2] - iex> b = [73, 37, 1] - iex> c = ShittyLinqEx.intersect(a, b) - [] - """ - - def intersect(list1, list2) do - list3 = list1 -- list2 - list4 = list1 -- list3 - end - -end \ No newline at end of file From 7a9741230f9246221d49a04f3dfd0c579a66a68d Mon Sep 17 00:00:00 2001 From: atmcmillon <56739546+atmcmillon@users.noreply.github.com> Date: Wed, 30 Sep 2020 22:46:29 -0500 Subject: [PATCH 14/22] Delete sum.exs --- lib/sum.exs | 51 --------------------------------------------------- 1 file changed, 51 deletions(-) delete mode 100644 lib/sum.exs diff --git a/lib/sum.exs b/lib/sum.exs deleted file mode 100644 index 636c2f0..0000000 --- a/lib/sum.exs +++ /dev/null @@ -1,51 +0,0 @@ -defmodule ShittyLinqEx do - @moduledoc """ - Documentation for `ShittyLinqEx`. - """ - - @doc """ - Filters a sequence of values based on a predicate. - Where `source` is an enumerable to filter. - Where `predicate` is a function to test each element for a condition. - Returns an enumerable that contains elements from the input sequence that satisfy the condition. - ## Examples - iex> import ShittyLinqEx, only: [where: 2] - iex> where([1, 2, 3], fn x -> rem(x, 2) == 0 end) - [2] - """ - def where(source, predicate) when is_list(source) do - where_list(source, predicate) - end - - defp where_list([head | tail], fun) do - case fun.(head) do - true -> [head | where_list(tail, fun)] - _ -> where_list(tail, fun) - end - end - - defp where_list([], _fun) do - [] - end - - #The sum() function - - @doc """ - Finds the sum of all values in a list with numeric elements. - - ##Examples - - iex> list = [1, 2, 3] - iex> shittyLinqEx.sum(list) - 6 - """ - - def sum([]) do - 0 - end - - def sum([h|t]) do - h + sum(t) - end - -end \ No newline at end of file From 08fac4d76e37ca10e1a2cd34054e7cb1bb31f648 Mon Sep 17 00:00:00 2001 From: atmcmillon <56739546+atmcmillon@users.noreply.github.com> Date: Wed, 30 Sep 2020 22:46:37 -0500 Subject: [PATCH 15/22] Delete union.exs --- lib/union.exs | 165 -------------------------------------------------- 1 file changed, 165 deletions(-) delete mode 100644 lib/union.exs diff --git a/lib/union.exs b/lib/union.exs deleted file mode 100644 index 5e677c9..0000000 --- a/lib/union.exs +++ /dev/null @@ -1,165 +0,0 @@ -defmodule ShittyLinqEx do - @moduledoc """ - Documentation for `ShittyLinqEx`. - """ - - @doc """ - Filters a sequence of values based on a predicate. - Where `source` is an enumerable to filter. - Where `predicate` is a function to test each element for a condition. - Returns an enumerable that contains elements from the input sequence that satisfy the condition. - ## Examples - iex> import ShittyLinqEx, only: [where: 2] - iex> where([1, 2, 3], fn x -> rem(x, 2) == 0 end) - [2] - """ - def where(source, predicate) when is_list(source) do - where_list(source, predicate) - end - - defp where_list([head | tail], fun) do - case fun.(head) do - true -> [head | where_list(tail, fun)] - _ -> where_list(tail, fun) - end - end - - defp where_list([], _fun) do - [] - end - - #The sum() function - - @doc """ - Finds the sum of all values in a list with numeric elements. - - ##Examples - - iex> list = [1, 2, 3] - iex> shittyLinqEx.sum(list) - 6 - """ - - def sum([]) do - 0 - end - - def sum([h|t]) do - h + sum(t) - end - - #The concat() function - - @doc """ - Places the elements of two lists into one(concatenate) - - ##Examples - - iex> a = [1, 2, 3] - iex> b = [4, 5, 6] - iex> shittyLINQ.concat(a, b) - [1, 2, 3, 4, 5, 6] - """ - def concat(list1, list2) do - list1 ++ list2 - end - - #The append() function - - @doc """ - Places a new element at the end of an existing list - - ##Examples - - iex> a = [1, 2, 3] - iex> b = shittyLinqEx.append(a, 4) - [1, 2, 3, 4] - iex> c = shittyLinqEx.append(b, 327) - [1, 2, 3, 4, 327] - """ - - def append(list, new) do - list ++ [new] - end - - #The count() function - @doc """ - Finds the number of elements in a list - - ##Examples - - iex> a = [1, 2, 3] - iex> shittyLinqEx.count(a) - 3 - """ - def count(list) do - length(list) - end - - #The average() function - @doc """ - Finds the arithmetic average of a list of numbers - - ##Examples - - iex> a = [1, 2, 3] - iex> shittyLinqEx.average(a) - 2 - """ - - def average([]) do - 0 - end - - def average(list) do - sum(list) / count(list) - end - - #The intersect() function - - @doc """ - Finds the intersection of 2 lists(where they have elements in common) - - ##Examples: - - iex> a = [1, 2, 3, 4] - iex> b = [2, 3, 4, 5] - iex> c = ShittyLinqEx.intersect(a, b) - [2, 3, 4] - - iex> a = [6, 42, 2] - iex> b = [73, 37, 1] - iex> c = ShittyLinqEx.intersect(a, b) - [] - """ - - def intersect(list1, list2) do - list3 = list1 -- list2 - list4 = list1 -- list3 - end - - #The union() function - - @doc """ - - Finds the union of two sets(combines both sets and excludes duplicates) - - ##Examples - - iex> a = [1, 37, 73] - iex> b = [1, 37, 97, 79] - iex> ShittyLinqEx.union(a, b) - [1, 37, 73, 97, 79] - """ - - def union(a, b) do - c = a -- b - d = b -- a - e = c ++ d - f = a -- c - g = f ++ e - end - - #The order_by() function - -end \ No newline at end of file From f344b59e2db835e1ebf9afc44d6c3db8161be06b Mon Sep 17 00:00:00 2001 From: atmcmillon <56739546+atmcmillon@users.noreply.github.com> Date: Wed, 30 Sep 2020 22:49:32 -0500 Subject: [PATCH 16/22] Update shitty_linq_ex.ex --- lib/shitty_linq_ex.ex | 63 ------------------------------------------- 1 file changed, 63 deletions(-) diff --git a/lib/shitty_linq_ex.ex b/lib/shitty_linq_ex.ex index 088af6f..8bc8a65 100644 --- a/lib/shitty_linq_ex.ex +++ b/lib/shitty_linq_ex.ex @@ -53,67 +53,4 @@ defmodule ShittyLinqEx do h + sum(t) end -#The concat() function - - @doc """ - Places the elements of two lists into one(concatenate) - - ##Examples - - iex> a = [1, 2, 3] - iex> b = [4, 5, 6] - iex> shittyLINQ.concat(a, b) - [1, 2, 3, 4, 5, 6] - """ - def concat(list1, list2) do - list1 ++ list2 - end - - #The append() function - - @doc """ - Places a new element at the end of an existing list - - ##Examples - - iex> a = [1, 2, 3] - iex> b = shittyLinqEx.append(a, 4) - [1, 2, 3, 4] - iex> c = shittyLinqEx.append(b, 327) - [1, 2, 3, 4, 327] - """ - def append(list, new) do - list ++ [new] - end - - #The count() function - @doc """ - Finds the number of elements in a list - - ##Examples - - iex> a = [1, 2, 3] - iex> shittyLinqEx.count(a) - 3 - """ - def count(list) do - length(list) - end - - #Average - - def average([]) do - 0 - end - - def average(list) do - sum(list) / count(list) - end - - #Union - - def union([h1|t1], [h2|t2]) do - where(concat([h1|t1], [h2|t2]) - end - end From 0164422cda0de1a352b213d92ad43d3d9a209d83 Mon Sep 17 00:00:00 2001 From: atmcmillon <56739546+atmcmillon@users.noreply.github.com> Date: Wed, 30 Sep 2020 23:46:54 -0500 Subject: [PATCH 17/22] Update shitty_linq_ex.ex --- lib/shitty_linq_ex.ex | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/lib/shitty_linq_ex.ex b/lib/shitty_linq_ex.ex index 8bc8a65..fca20d9 100644 --- a/lib/shitty_linq_ex.ex +++ b/lib/shitty_linq_ex.ex @@ -2,7 +2,27 @@ defmodule ShittyLinqEx do @moduledoc """ Documentation for `ShittyLinqEx`. """ + + #The sum() function + + @doc """ + Finds the sum of all values in a list with numeric elements. + + ##Examples + + iex> list = [1, 2, 3] + iex> shittyLinqEx.sum(list) + 6 + """ + def sum([]) do + 0 + end + + def sum([h|t]) do + h + sum(t) + end + @doc """ Filters a sequence of values based on a predicate. @@ -33,24 +53,4 @@ defmodule ShittyLinqEx do [] end - #The sum() function - - @doc """ - Finds the sum of all values in a list with numeric elements. - - ##Examples - - iex> list = [1, 2, 3] - iex> shittyLinqEx.sum(list) - 6 - """ - - def sum([]) do - 0 - end - - def sum([h|t]) do - h + sum(t) - end - end From 43dab6c5a86eacf24959d37e7abe8c10614e9af6 Mon Sep 17 00:00:00 2001 From: atmcmillon <56739546+atmcmillon@users.noreply.github.com> Date: Thu, 1 Oct 2020 17:19:20 -0500 Subject: [PATCH 18/22] Update shitty_linq_ex.ex --- lib/shitty_linq_ex.ex | 22 +++------------------- 1 file changed, 3 insertions(+), 19 deletions(-) diff --git a/lib/shitty_linq_ex.ex b/lib/shitty_linq_ex.ex index 8022f46..cfac289 100644 --- a/lib/shitty_linq_ex.ex +++ b/lib/shitty_linq_ex.ex @@ -3,15 +3,13 @@ defmodule ShittyLinqEx do Documentation for `ShittyLinqEx`. """ - #The sum() function - @doc """ Finds the sum of all values in a list with numeric elements. - ##Examples + ## Examples iex> list = [1, 2, 3] - iex> shittyLinqEx.sum(list) + iex> ShittyLinqEx.sum(list) 6 """ @@ -46,14 +44,10 @@ defmodule ShittyLinqEx do [0, 20, 15, 40] """ - def where(source, predicate) when is_list(source) and is_function(predicate, 1) do + def where(source, predicate) when is_list(source) do where_list(source, predicate) end - def where(source, predicate) when is_list(source) and is_function(predicate, 2) do - where_list(source, predicate, 0) - end - defp where_list([head | tail], fun) do case fun.(head) do true -> [head | where_list(tail, fun)] @@ -65,14 +59,4 @@ defmodule ShittyLinqEx do [] end - defp where_list([head | tail], fun, index) do - case fun.(head, index) do - true -> [head | where_list(tail, fun, index + 1)] - _ -> where_list(tail, fun, index + 1) - end - end - - defp where_list([], _fun, _index) do - [] - end end From 06d32c44fbff875dd5ff64d1bed30ffc5b11cf44 Mon Sep 17 00:00:00 2001 From: atmcmillon <56739546+atmcmillon@users.noreply.github.com> Date: Sat, 3 Oct 2020 17:08:09 -0500 Subject: [PATCH 19/22] Update shitty_linq_ex.ex --- lib/shitty_linq_ex.ex | 252 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 240 insertions(+), 12 deletions(-) diff --git a/lib/shitty_linq_ex.ex b/lib/shitty_linq_ex.ex index cfac289..dc282b7 100644 --- a/lib/shitty_linq_ex.ex +++ b/lib/shitty_linq_ex.ex @@ -2,6 +2,208 @@ defmodule ShittyLinqEx do @moduledoc """ Documentation for `ShittyLinqEx`. """ + + @doc """ + Applies an accumulator function over a sequence. The specified seed value is used as the initial + accumulator value, and the specified function is used to select the result value. + ## Parameters + - `source`: an enumerable to aggregate over. + - `seed`: the initial accumulator value. + - `func`: an accumulator function to be invoked on each element. + - `resultSelector`: a function to transform the final accumulator value into the result value. + ## Returns + The transformed final accumulator value. + ## Examples + iex> import ShittyLinqEx, only: [aggregate: 4] + iex> fruits = ["apple", "mango", "orange", "passionfruit", "grape"] + iex> aggregate( + ...> fruits, + ...> "banana", + ...> fn next, longest -> + ...> if String.length(next) > String.length(longest) do + ...> next + ...> else + ...> longest + ...> end + ...> end, + ...> &String.upcase/1) + "PASSIONFRUIT" + """ + def aggregate(source, seed, func, resultSelector) + when is_list(source) and is_function(func, 2) and is_function(resultSelector, 1) do + :lists.foldl(func, seed, source) + |> resultSelector.() + end + + def aggregate(first..last, seed, func, resultSelector) + when is_function(func, 2) and is_function(resultSelector, 1) do + if first <= last do + aggregate_range_inc(first, last, seed, func) + else + aggregate_range_dec(first, last, seed, func) + end + |> resultSelector.() + end + + def aggregate(%{} = source, seed, func, resultSelector) + when is_function(func, 2) and is_function(resultSelector, 1) do + :maps.fold( + fn key, value, accumulator -> func.({key, value}, accumulator) end, + seed, + source + ) + |> resultSelector.() + end + + def aggregate(source, seed, func, resultSelector) + when is_list(source) and is_function(func, 2) and is_function(resultSelector, 1) do + :lists.foldl(func, seed, source) + |> resultSelector.() + end + + def aggregate(first..last, seed, func, resultSelector) + when is_function(func, 2) and is_function(resultSelector, 1) do + if first <= last do + aggregate_range_inc(first, last, seed, func) + else + aggregate_range_dec(first, last, seed, func) + end + |> resultSelector.() + end + + @doc """ + Applies an accumulator function over a sequence. + The specified seed value is used as the initial accumulator value. + ## Parameters + - `source`: an enumerable to aggregate over. + - `seed`: the initial accumulator value. + - `func`: an accumulator function to be invoked on each element. + ## Returns + The final accumulator value. + ## Examples + iex> import ShittyLinqEx, only: [aggregate: 3] + iex> ints = [4, 8, 8, 3, 9, 0, 7, 8, 2] + iex> aggregate( + ...> ints, + ...> 0, + ...> fn next, total -> + ...> if rem(next, 2) == 0 do + ...> total + 1 + ...> else + ...> total + ...> end + ...> end) + 6 + iex> import ShittyLinqEx, only: [aggregate: 3] + iex> aggregate(4..1, 1, &*/2) + 24 + iex> import ShittyLinqEx, only: [aggregate: 3] + iex> aggregate(1..3, 1, &+/2) + 7 + iex> import ShittyLinqEx, only: [aggregate: 3] + iex> letters_to_numbers = %{a: 1, b: 2, c: 3} + iex> aggregate( + ...> letters_to_numbers, + ...> [], + ...> fn {key, _value}, keys -> [key | keys] end) + [:c, :b, :a] + """ + def aggregate(source, seed, func) + when is_list(source) and is_function(func, 2) do + :lists.foldl(func, seed, source) + end + + def aggregate(first..last, seed, func) + when is_function(func, 2) do + if first <= last do + aggregate_range_inc(first, last, seed, func) + else + aggregate_range_dec(first, last, seed, func) + end + end + + def aggregate(%{} = source, seed, func) + when is_function(func, 2) do + :maps.fold( + fn key, value, acc -> func.({key, value}, acc) end, + seed, + source + ) + end + + @doc """ + Applies an accumulator function over a sequence. + ## Parameters + - `source`: an enumerable to aggregate over. + - `func`: an accumulator function to be invoked on each element. + ## Returns + The final accumulator value. + ## Examples + iex> import ShittyLinqEx, only: [aggregate: 2] + iex> sentence = "the quick brown fox jumps over the lazy dog" + iex> words = String.split(sentence) + iex> aggregate( + ...> words, + ...> fn word, workingSentence -> word <> " " <> workingSentence end) + "dog lazy the over jumps fox brown quick the" + """ + def aggregate([head | tail], func) + when is_function(func, 2) do + aggregate(tail, head, func) + end + + @doc """ + Inverts the order of the elements in a sequence. + ## Parameters + - `list`: A sequence of values to reverse. + ## Returns + A sequence whose elements correspond to those of the input sequence in reverse order. + ## Examples + iex> import ShittyLinqEx, only: [reverse: 1] + iex> reverse(["A", "B", "C"]) + ["C", "B", "A"] + iex> import ShittyLinqEx, only: [reverse: 1] + iex> reverse([42, "orange", ":atom"]) + [":atom", "orange", 42] + """ + + @spec reverse(list) :: list + def reverse(list) when is_list(list), do: reverse(list, []) + def reverse([head | tail], acc), do: reverse(tail, [head | acc]) + def reverse([], acc), do: acc + + @doc """ + Returns the first element of a sequence. + ## Parameters + - `list`: A sequence of values of which the first element should be returned. + - `predicate`: A function to check for each element + - `value`: A value which will be checked in the predicate function + ## Returns + First value of the input sequence. + ## Examples + iex> import ShittyLinqEx, only: [first: 1] + iex> first(["A", "B", "C"]) + "A" + iex> import ShittyLinqEx, only: [first: 1] + iex> first([42, "orange", ":atom"]) + 42 + iex> import ShittyLinqEx, only: [first: 3] + iex> first([4, 2, 3], &>/2, 1) + 4 + """ + + def first(list) when is_list(list), do: List.first(list) + def first([]), do: nil + def first(nil), do: nil + + def first([head | tail], func, value) when is_list(tail) and is_function(func, 2) do + case func.(head, value) do + true -> head + false -> first(tail, func, value) + end + end + + def first([], _func, _value), do: nil @doc """ Finds the sum of all values in a list with numeric elements. @@ -18,36 +220,52 @@ defmodule ShittyLinqEx do end def sum([h|t]) do - h + sum(t) + h + sum(t) end - + @doc """ Filters a sequence of values based on a predicate. - - Where `source` is an enumerable to filter. - Where `predicate` is a function to test each element for a condition. - - Returns an enumerable that contains elements from the input sequence that satisfy the condition. - + ## Parameters + - `source`: an enumerable to filter. + - `predicate`: a function to test each element for a condition. + ## Returns + An enumerable that contains elements from the input sequence that satisfy the condition. ## Examples - iex> import ShittyLinqEx, only: [where: 2] iex> where( ...> ["apple", "passionfruit", "banana", "mango", "orange", "blueberry", "grape", "strawberry"], ...> fn fruit -> String.length(fruit) < 6 end) ["apple", "mango", "grape"] - iex> import ShittyLinqEx, only: [where: 2] iex> where( ...> [0, 30, 20, 15, 90, 85, 40, 75], ...> fn number, index -> number <= index * 10 end) [0, 20, 15, 40] - """ - def where(source, predicate) when is_list(source) do + def where(source, predicate) when is_list(source) and is_function(predicate, 1) do where_list(source, predicate) end + def where(source, predicate) when is_list(source) and is_function(predicate, 2) do + where_list(source, predicate, 0) + end + + defp aggregate_range_inc(first, first, seed, func) do + func.(first, seed) + end + + defp aggregate_range_inc(first, last, seed, func) do + aggregate_range_inc(first + 1, last, func.(first, seed), func) + end + + defp aggregate_range_dec(first, first, seed, func) do + func.(first, seed) + end + + defp aggregate_range_dec(first, last, seed, func) do + aggregate_range_dec(first - 1, last, func.(first, seed), func) + end + defp where_list([head | tail], fun) do case fun.(head) do true -> [head | where_list(tail, fun)] @@ -59,4 +277,14 @@ defmodule ShittyLinqEx do [] end + defp where_list([head | tail], fun, index) do + case fun.(head, index) do + true -> [head | where_list(tail, fun, index + 1)] + _ -> where_list(tail, fun, index + 1) + end + end + + defp where_list([], _fun, _index) do + [] + end end From 07239712ba17a0bdea07a96188e97e8107d36260 Mon Sep 17 00:00:00 2001 From: atmcmillon <56739546+atmcmillon@users.noreply.github.com> Date: Sat, 3 Oct 2020 17:22:39 -0500 Subject: [PATCH 20/22] Update shitty_linq_ex.ex --- lib/shitty_linq_ex.ex | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/shitty_linq_ex.ex b/lib/shitty_linq_ex.ex index a96014d..288e70e 100644 --- a/lib/shitty_linq_ex.ex +++ b/lib/shitty_linq_ex.ex @@ -171,6 +171,22 @@ defmodule ShittyLinqEx do def reverse(list) when is_list(list), do: reverse(list, []) def reverse([head | tail], acc), do: reverse(tail, [head | acc]) def reverse([], acc), do: acc + + @doc """ + Places a new element at the end of an existing list + + ## Examples + + iex> a = [1, 2, 3] + iex> b = ShittyLinqEx.append(a, 4) + [1, 2, 3, 4] + iex> c = ShittyLinqEx.append(b, 327) + [1, 2, 3, 4, 327] + """ + + def append(list, new) do + list ++ [new] + end @doc """ Returns the first element of a sequence. @@ -287,4 +303,4 @@ defmodule ShittyLinqEx do defp where_list([], _fun, _index) do [] end -end \ No newline at end of file +end From 41c19548fb0c3eb7b10b940a856cc2aa4535a66d Mon Sep 17 00:00:00 2001 From: atmcmillon <56739546+atmcmillon@users.noreply.github.com> Date: Sat, 3 Oct 2020 17:31:36 -0500 Subject: [PATCH 21/22] Update shitty_linq_ex.ex --- lib/shitty_linq_ex.ex | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/lib/shitty_linq_ex.ex b/lib/shitty_linq_ex.ex index 288e70e..31570b1 100644 --- a/lib/shitty_linq_ex.ex +++ b/lib/shitty_linq_ex.ex @@ -171,22 +171,6 @@ defmodule ShittyLinqEx do def reverse(list) when is_list(list), do: reverse(list, []) def reverse([head | tail], acc), do: reverse(tail, [head | acc]) def reverse([], acc), do: acc - - @doc """ - Places a new element at the end of an existing list - - ## Examples - - iex> a = [1, 2, 3] - iex> b = ShittyLinqEx.append(a, 4) - [1, 2, 3, 4] - iex> c = ShittyLinqEx.append(b, 327) - [1, 2, 3, 4, 327] - """ - - def append(list, new) do - list ++ [new] - end @doc """ Returns the first element of a sequence. @@ -221,6 +205,27 @@ defmodule ShittyLinqEx do def first([], _func, _value), do: nil + @doc """ + Finds the intersection of 2 lists(where they have elements in common) + + ## Examples: + + iex> a = [1, 2, 3, 4] + iex> b = [2, 3, 4, 5] + iex> c = ShittyLinqEx.intersect(a, b) + [2, 3, 4] + + iex> a = [6, 42, 2] + iex> b = [73, 37, 1] + iex> c = ShittyLinqEx.intersect(a, b) + [] + """ + + def intersect(list1, list2) do + list3 = list1 -- list2 + list4 = list1 -- list3 + end + @doc """ Filters a sequence of values based on a predicate. ## Parameters From fea568e0cbc33fb340de7d16d11758b170961c94 Mon Sep 17 00:00:00 2001 From: atmcmillon <56739546+atmcmillon@users.noreply.github.com> Date: Sat, 3 Oct 2020 17:42:15 -0500 Subject: [PATCH 22/22] Update shitty_linq_ex.ex --- lib/shitty_linq_ex.ex | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/lib/shitty_linq_ex.ex b/lib/shitty_linq_ex.ex index 31570b1..5bfac48 100644 --- a/lib/shitty_linq_ex.ex +++ b/lib/shitty_linq_ex.ex @@ -286,6 +286,26 @@ defmodule ShittyLinqEx do def sum([h|t]) do h + sum(t) end + + @doc """ + + Finds the union of two sets(combines both sets and excludes duplicates) + + ## Examples + + iex> a = [1, 37, 73] + iex> b = [1, 37, 97, 79] + iex> ShittyLinqEx.union(a, b) + [1, 37, 73, 97, 79] + """ + + def union(a, b) do + c = a -- b + d = b -- a + e = c ++ d + f = a -- c + g = f ++ e + end defp where_list([head | tail], fun) do case fun.(head) do