酷!先 mark 一下
可以呀,欢迎来分享。
已更新
欢迎对 elixir 感兴趣的朋友前来围观~ 这次我的演讲内容是 elixir/erlang 中的基础部分. 也欢迎各位大牛前来分享 elixir/erlang 的使用经验
1:30 ~ 2:00 我们会在研祥科技大厦一楼迎接。迟到请联系 13400665101
类似 erlang/elixir 里加类型的方式?
受益匪浅。
不过我现在已经专做后端了 我想问的是,一个好的后端工程师应该怎样与前端合作?
我的代码太可怕了。。。
刚学 elixir,还不太会用,勉强实现了。
defmodule Poker do
@ranks ["high card", "pair", "two pairs", "three of a kind", "straight",
"flush", "full house", "four of a kind", "straight flush"]
@values [2, 3, 4, 5, 6, 7, 8, 9, 10, "Jack", "Queen", "King", "Ace"]
def start(str) do
str
|> String.trim
|> String.split("\n")
|> Enum.map(&String.trim(&1, "Black: "))
|> Enum.map(&String.split(&1, " White: "))
|> Enum.map(fn([b, w]) -> compare(b, w) end)
end
def compare(b, w) do
[b, w]
|> Enum.map(&parse/1)
|> winner_is
end
defp parse(str) do
str
|> String.trim
|> String.split("\s")
|> Enum.map(fn
<<?A, s>> -> [ 14, s]
<<?K, s>> -> [ 13, s]
<<?Q, s>> -> [ 12, s]
<<?J, s>> -> [ 11, s]
<<?T, s>> -> [ 10, s]
<<x, s>> -> [x-?0, s]
end)
|> Enum.sort(&>/2)
|> rank_and_value
end
defp rank_and_value(cards) do
x = hd(hd(cards))
y = x-1
z = x-2
u = x-3
v = x-4
case cards do
[[^x,a],[^y,a],[^z,a],[^u,a],[^v,a]] -> [rank: 9, value: x]
[[x,_],[x,_],[x,_],[x,_],[y,_]] -> [rank: 8, value: x]
[[x,_],[y,_],[y,_],[y,_],[y,_]] -> [rank: 8, value: y]
[[x,_],[x,_],[x,_],[y,_],[y,_]] -> [rank: 7, value: x]
[[x,_],[x,_],[y,_],[y,_],[y,_]] -> [rank: 7, value: y]
[[x,a],[_,a],[_,a],[_,a],[_,a]] -> [rank: 6, value: x]
[[^x,_],[^y,_],[^z,_],[^u,_],[^v,_]] -> [rank: 5, value: x]
[[x,_],[x,_],[x,_],[y,_],[z,_]] -> [rank: 4, value: x]
[[x,_],[y,_],[z,_],[z,_],[z,_]] -> [rank: 4, value: z]
[[x,_],[x,_],[y,_],[y,_],[z,_]] -> [rank: 3, value: [x,y,z]]
[[x,_],[x,_],[y,_],[z,_],[z,_]] -> [rank: 3, value: [x,z,y]]
[[x,_],[y,_],[y,_],[z,_],[z,_]] -> [rank: 3, value: [y,z,x]]
[[x,_],[x,_],[y,_],[z,_],[u,_]] -> [rank: 2, value: [x,y,z,u]]
[[x,_],[y,_],[y,_],[z,_],[u,_]] -> [rank: 2, value: [y,x,z,u]]
[[x,_],[y,_],[z,_],[z,_],[u,_]] -> [rank: 2, value: [z,x,y,u]]
[[x,_],[y,_],[z,_],[u,_],[u,_]] -> [rank: 2, value: [u,x,y,z]]
[[x,_],[y,_],[z,_],[u,_],[v,_]] -> [rank: 1, value: [x,y,z,u,v]]
end
end
defp winner_is([black, white]) do
black_rank = Enum.at(@ranks, black[:rank]-1)
white_rank = Enum.at(@ranks, white[:rank]-1)
cond do
black[:rank] > white[:rank] ->
IO.puts "Black wins. - with #{black_rank}"
black[:rank] < white[:rank] ->
IO.puts "White wins. - with #{white_rank}"
true -> value_compare([black, white])
end
end
defp value_compare([black, white]) do
black_rank = Enum.at(@ranks, black[:rank]-1)
white_rank = Enum.at(@ranks, white[:rank]-1)
cond do
black[:value] > white[:value] ->
black_value = value_message(black[:value], white[:value])
IO.puts "Black wins. - with #{black_rank}: #{black_value}"
black[:value] < white[:value] ->
white_value = value_message(white[:value], black[:value])
IO.puts "White wins. - with #{white_rank}: #{white_value}"
true -> IO.puts "Tie."
end
end
defp value_message(a,b) do
unless is_list(a) do
Enum.at(@values, a-2)
else
Enum.at(@values, first_uniq_value(a, b)-2)
end
end
defp first_uniq_value(a, b) when hd(a) == hd(b) do
first_uniq_value(tl(a), tl(b))
end
defp first_uniq_value(a, b), do: hd(a)
end
IO.inspect Poker.start(
"Black: 2H 3D 5S 9C KD White: 2C 3H 4S 8C AH
Black: 2H 4S 4C 2D 4H White: 2S 8S AS QS 3S
Black: 2H 3D 5S 9C KD White: 2C 3H 4S 8C KH
Black: 2H 3D 5S 9C KD White: 2D 3H 5C 9S KH")
输出
White wins. - with high card: Ace
Black wins. - with full house
Black wins. - with high card: 9
Tie.
[:ok, :ok, :ok, :ok]
已翻译完成
String,Module,Class 都是类的名字,所以它们都是 Class 类。这是在下愚见。
看起来很不错,想去实习~目前正在学习 Rails 我的博客 https://ljzn.github.io/ 每天更新噢