3 Main States:
- foo
- bar
- substate 1
- substate 2
- substate 3
- baz
An application-wide menu bar has buttons for foo, bar, and baz.
require 'json' | |
require 'rest_client' | |
reddit_hash = JSON.parse(RestClient.get('http://reddit.com/.json')) | |
posts = reddit_hash["data"]["children"] | |
#remove all posts which are NSFW | |
posts = posts.select { |post| post["data"]["over_18"] != true } | |
require 'json' | |
require 'rest_client' | |
class Story | |
attr_accessor :upvotes, :downvotes, :permalink, :title, :thumbnail, :over_18 | |
@@stories = [] | |
def initialize(post) | |
self.upvotes = post["data"]["ups"] | |
self.downvotes = post["data"]["downs"] |
/* JS Happy Number Finder | |
Run from command line with node happyNumberFinder.js [number] | |
*/ | |
var _ = require('underscore'); | |
var HappyNumberFinder = (function(_){ | |
"use strict"; |
3 Main States:
An application-wide menu bar has buttons for foo, bar, and baz.
defmodule HappyNumberFinder do | |
def get_sum(number) do | |
numbers = Integer.digits(number) #=> [4, 9] | |
squared_numbers = Enum.map(numbers, fn(x) -> x * x end) #=> [16, 81] | |
sum = Enum.reduce(squared_numbers, fn(x, total) -> x + total end) #=> 97 | |
end | |
end |
defmodule HappyNumberFinder do | |
def get_sum(number) do | |
number | |
|> Integer.digits #=> [4, 9] | |
|> Enum.map(fn(x) -> x * x end) #=> [16, 81] | |
|> Enum.reduce(fn(x, total) -> x + total end) #=> 97 | |
end | |
end |
defmodule HappyNumberFinder do | |
def get_sum(number) do | |
number | |
|> Integer.digits #=> [4, 9] | |
|> Enum.map(&(&1 * &1)) #=> [16, 81] | |
|> Enum.reduce(&(&1 + &2)) #=> 97 | |
end | |
end |
defmodule HappyNumberFinder do | |
# def get_sum(number) | |
def find(number) do | |
sum = get_sum(number) | |
case sum do | |
1 -> {:happy} | |
_ -> find(sum) |
defmodule HappyNumberFinder do | |
# def get_sum(number) | |
def find(number) do | |
_find(number, []) | |
end | |
defp _find(number, guesses) do | |
sum = get_sum(number) |
defmodule HappyNumberFinder do | |
# def get_sum(number) | |
def find(number) do | |
_find(number, []) | |
end | |
defp _find(number, guesses) where number in guesses do | |
{:unhappy} |