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

Atmcmillon union patch 1 #64

Open
wants to merge 26 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
a139524
Update shitty_linq_ex.ex
atmcmillon Sep 28, 2020
185c28a
Update shitty_linq_ex.ex
atmcmillon Sep 28, 2020
19507ee
Update shitty_linq_ex.ex
atmcmillon Sep 28, 2020
c0d3c86
Update shitty_linq_ex.ex
atmcmillon Sep 28, 2020
564cb32
Update shitty_linq_ex.ex
atmcmillon Sep 28, 2020
0998dcf
Update shitty_linq_ex.ex
atmcmillon Sep 28, 2020
500c430
Update shitty_linq_ex.ex
atmcmillon Sep 29, 2020
e021b5b
Add files via upload
atmcmillon Oct 1, 2020
b665644
Delete append.exs
atmcmillon Oct 1, 2020
1663f33
Delete average.exs
atmcmillon Oct 1, 2020
f3a17d4
Delete concat.exs
atmcmillon Oct 1, 2020
e42ba6d
Delete count.exs
atmcmillon Oct 1, 2020
c7b5b4a
Delete intersect.exs
atmcmillon Oct 1, 2020
7a97412
Delete sum.exs
atmcmillon Oct 1, 2020
08fac4d
Delete union.exs
atmcmillon Oct 1, 2020
f344b59
Update shitty_linq_ex.ex
atmcmillon Oct 1, 2020
0164422
Update shitty_linq_ex.ex
atmcmillon Oct 1, 2020
df7a4d9
Merge branch 'master' into master
the-pat Oct 1, 2020
43dab6c
Update shitty_linq_ex.ex
atmcmillon Oct 1, 2020
06d32c4
Update shitty_linq_ex.ex
atmcmillon Oct 3, 2020
c2c8528
Merge branch 'master' into master
atmcmillon Oct 3, 2020
0723971
Update shitty_linq_ex.ex
atmcmillon Oct 3, 2020
3121cb8
Merge pull request #2 from atmcmillon/atmcmillon-append-patch-2
atmcmillon Oct 3, 2020
41c1954
Update shitty_linq_ex.ex
atmcmillon Oct 3, 2020
e04a3d5
Merge pull request #3 from atmcmillon/atmcmillon-intersect-patch-1
atmcmillon Oct 3, 2020
fea568e
Update shitty_linq_ex.ex
atmcmillon Oct 3, 2020
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
108 changes: 59 additions & 49 deletions lib/shitty_linq_ex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,14 @@ defmodule ShittyLinqEx do
@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(
Expand All @@ -34,7 +28,6 @@ defmodule ShittyLinqEx do
...> 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
Expand Down Expand Up @@ -81,19 +74,13 @@ defmodule ShittyLinqEx do
@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(
Expand All @@ -107,23 +94,19 @@ defmodule ShittyLinqEx do
...> 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
Expand All @@ -150,26 +133,19 @@ defmodule ShittyLinqEx do

@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
Expand All @@ -178,25 +154,17 @@ defmodule ShittyLinqEx do

@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
Expand All @@ -206,31 +174,22 @@ defmodule ShittyLinqEx do

@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)
Expand All @@ -247,31 +206,44 @@ defmodule ShittyLinqEx do
def first([], _func, _value), do: nil

@doc """
Filters a sequence of values based on a predicate.
Finds the intersection of 2 lists(where they have elements in common)

## Parameters
## 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
- `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) and is_function(predicate, 1) do
where_list(source, predicate)
Expand All @@ -297,6 +269,44 @@ defmodule ShittyLinqEx do
aggregate_range_dec(first - 1, last, func.(first, seed), func)
end

@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 """

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
true -> [head | where_list(tail, fun)]
Expand Down