Skip to content

Instantly share code, notes, and snippets.

# Use this code and data to answer the questions.
defmodule Millenium.Falcon do
def rebel?(c = %{rebel: true}) do
c.type != :droid
end
def rebel?(_), do: false
def fill_ship(characters) do
characters = [
%{name: "Han", type: :human, rebel: true, weight: 185},
%{name: "Jabba", type: :hutt, rebel: false, weight: 2200},
%{name: "Chewie", type: :wookie, rebel: true, weight: 350},
%{name: "r2d2", type: :droid, rebel: true, weight: 250},
%{name: "Luke", type: :human, rebel: true, weight: 140},
%{name: "Boba Fett", type: :human, rebel: false, weight: 175},
]
# Note that there is more than one correct answer for all of these!
# Create a variable has_seen_star_wars and set it to true.
has_seen_star_wars = true
# Write code that prints "Ready for class" if you have seen at least one Star Wars movie (has_seen_star_wars).
if has_seen_star_wars do
IO.puts "Ready for class"
end
# Flip has_seen_star_wars to false.
has_seen_star_wars = false
defmodule WhatHappensIn_1_3 do
def some_func() do
receive do
{from, message} ->
send from, "hello #{inspect message}"
some_func()
_ ->
IO.puts "quittin' time"
end
# this is using sweet_xml
xml_string = File.read!("some_file.xml")
try do
Logger.info(self()) # it does not work without this ... wat?
{:ok, xml} = Parser.parse(xml_string)
process_xml(conn, xml_string, xml)
rescue
e ->
Logger.info("ignoring unparseable XML file: #{inspect xml_string}")
render(conn, "empty.json")
@brweber2
brweber2 / custom_sigil.ex
Created September 25, 2015 13:28
Custom Sigil with pattern matching...
def magic(nbr) do
a = ~x(a1b2c3d4)
b = ~x(d4c3b2a1)
c = ~x(a1b23c4d)
d = ~x(4d3cb2a1)
case nbr do
^a -> %Pcap.Global.MagicNumber{ magic_number: nbr, swap_fields: false, nanoseconds: false }
^b -> %Pcap.Global.MagicNumber{ magic_number: nbr, swap_fields: true, nanoseconds: false }
^c -> %Pcap.Global.MagicNumber{ magic_number: nbr, swap_fields: false, nanoseconds: true }
^d -> %Pcap.Global.MagicNumber{ magic_number: nbr, swap_fields: true, nanoseconds: true }
@brweber2
brweber2 / Benum.ex
Last active April 4, 2022 03:38
Elixir Enumerable for binaries....
defmodule Benum do
defimpl Enumerable, for: BitString do
def count(collection) when is_binary(collection) do
{:ok, Enum.reduce(collection, 0, fn v, acc -> acc + 1 end)}
end
def count(collection) do
{:error, __MODULE__}
end
def member?(collection, value) when is_binary(collection) do
{:ok, Enum.any?(collection, fn v -> value == v end)}
vagrant@vagrant-ubuntu-trusty-64:~/irr/irr$ mix deps.compile
==> gen_smtp (compile)
Compiling src/smtp_rfc822_parse.yrl failed:
ERROR: compile failed while processing /home/vagrant/irr/irr/deps/gen_smtp: rebar_abort
** (Mix) Could not compile dependency gen_smtp, /home/vagrant/.mix/rebar command failed. If you want to recompile this dependency, please run: mix deps.compile gen_smtp
defmodule Permutations do
@moduledoc """
An answer to the problem from: http://www.theguardian.com/science/alexs-adventures-in-numberland/2015/may/20/can-you-do-the-maths-puzzle-for-vietnamese-eight-year-olds-that-has-stumped-parents-and-teachers
iex> Permutations.main parallel: false
iex> Permutations.main parallel: true
"""
@brweber2
brweber2 / view_helpers.ex
Last active September 30, 2015 01:54
Phoenix View Helpers for select boxes
defmodule Clockout.MyViewHelpers do
def display_name(:client, id) do
Clockout.Client |> display_name_by_id(id)
end
def display_name(:person, id) do
Clockout.Person |> display_name_by_id(id)
end