This file contains hidden or 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
| #!/usr/bin/env ruby | |
| require 'net/http' | |
| require 'json' | |
| def get_json(url) | |
| uri = URI(url) | |
| JSON.parse(Net::HTTP.get uri) | |
| end |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or 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
| defmodule MatchAndAssign do | |
| defmacro __using__(_) do | |
| quote do | |
| require MatchAndAssign | |
| import MatchAndAssign, only: [match_and_assign: 2] | |
| end | |
| end | |
| defp unhygienize(pattern = {n, _meta, c}) when is_atom(n) and is_atom(c) do | |
| var! pattern |
This file contains hidden or 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
| defmodule LazyPermutations do | |
| def permutations(list) do | |
| list | |
| |> Enum.sort | |
| |> Stream.unfold fn | |
| [] -> nil | |
| p -> {p, next_permutation(p)} | |
| end | |
| end |
This file contains hidden or 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
| defmodule XStream do | |
| @doc """ | |
| Returns a stream of all permutations of the given collection. | |
| The count parameter will limit the number of elements, all are returned | |
| by default | |
| ## Examples |
This file contains hidden or 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
| defmodule Between do | |
| @doc """ | |
| ## Examples | |
| iex> list = for x <- 1000..1031, rem(x, 5) != 0, do: x | |
| iex> chunks = Between.chunk_between(list, fn a, b -> b - a != 1 end) | |
| iex> IO.inspect | |
| [[1001, 1002, 1003, 1004], [1006, 1007, 1008, 1009], [1011, 1012, 1013, 1014], | |
| [1016, 1017, 1018, 1019], [1021, 1022, 1023, 1024], [1026, 1027, 1028, 1029], | |
| [1031]] |
This file contains hidden or 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
| $ elixir tuplify.exs | |
| Returned from macro: | |
| def(tuplify_from_macro(x)) do | |
| {unquote(Macro.var(:x, nil))} | |
| end | |
| ** (CompileError) tuplify.exs:21: function x/0 undefined | |
| (stdlib) lists.erl:1336: :lists.foreach/2 | |
| tuplify.exs:14: (file) | |
| (elixir) lib/code.ex:307: Code.require_file/2 | |
This file contains hidden or 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
| defmodule Bubble do | |
| def sort(list) do | |
| do_sort list, {[], false} | |
| end | |
| defp do_sort([a, b | tail], {acc, _}) when b < a do | |
| do_sort [a | tail], {[b | acc], true} | |
| end | |
| defp do_sort([a | tail], {acc, swapped?}) do |
This file contains hidden or 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
| require 'minitest/autorun' | |
| module ConvexHull | |
| class << self | |
| def find(points) | |
| sorted = points.sort.uniq | |
| reversed = sorted.reverse | |
| [sorted, reversed].map do |points| | |
| points. | |
| reduce([]) {|acc, p| add_to_convex_list p, acc }. |
This file contains hidden or 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
| defmodule ConvexHull do | |
| @type coordinate :: {number, number} | |
| @type point_list :: [coordinate] | |
| @spec find(point_list) :: point_list | |
| def find(points) do | |
| sorted = points |> Enum.sort | |
| left = sorted |> do_half_calc | |
| right = sorted |> Enum.reverse |> do_half_calc | |
| [left, right] |> Enum.concat |