Last active
January 15, 2019 03:58
-
-
Save taiansu/6814f6fe3eaf0171b5f87d9a028dd751 to your computer and use it in GitHub Desktop.
functional practice
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
students = [ | |
{age: 18, first_name: "Shane", last_name: "Osbourne"}, | |
{age: 22, first_name: "John", last_name: "Doe"}, | |
{age: 12, first_name: "John", last_name: "Barker"}, | |
{age: 40, first_name: "Ben", last_name: "Haskell"}, | |
{age: 34, first_name: "Ben", last_name: "Barker"}, | |
{age: 9, first_name: "Shane", last_name: "Doe"}, | |
{age: 33, first_name: "Lee", last_name: "Ruby"} | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
students | |
|> Enum.filter(fn s -> s.age > 20 end) | |
|> Enum.group_by(fn s -> s.first_name end) | |
|> Enum.map(fn {k, v} -> | |
last_names = Enum.map(v, fn s -> s.last_name end) | |
{k, last_names} | |
end) | |
|> Enum.into(%{}) | |
|> IO.inspect |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let result = R.compose( | |
R.fromPairs, | |
R.map(([k, v]) => { | |
let last_names = R.map(s => s.last_name, v) | |
return [k, last_names] | |
}), | |
R.toPairs, | |
R.groupBy(s => s.first_name), | |
R.filter(s => s.age > 20) | |
)(students); | |
p result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
result = | |
students.filter { |s| s[:age] > 20} | |
.group_by { |s| s[:first_name] } | |
.map do |key, val| # 也可以改用 hash.transform_values | |
last_names = val.map { |s| s[:last_name] } | |
[key, last_names] | |
end | |
.to_h | |
p result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
students | |
|> Enum.filter(fn s -> s.age > 20 end) | |
|> Enum.reduce(%{}, fn %{first_name: first_name, last_name: last_name}, acc -> | |
prev = acc |> Map.get(first_name) |> List.wrap() | |
Map.put(acc, first_name, [last_name | prev]) | |
end) | |
|> IO.inspect() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let result = R.compose( | |
R.reduce((accu, {first_name, last_name}) => { | |
let prev = R.propOr([], first_name, accu) | |
return R.assoc(first_name, [...prev, last_name], accu); | |
}, {}), | |
R.filter(s => s.age > 20) | |
)(students); | |
console.log(result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
result = | |
students.filter { |s| s[:age] > 20 } | |
.reduce({}) do |accu, s| | |
first_name, last_name = s.values_at(:first_name, :last_name) | |
prev = Array(accu[first_name]) | |
accu[first_name] = [*prev, last_name] | |
accu | |
end | |
p result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment