Counter::Application.routes.draw do
match '/incr' => 'counter#incr', via: :post
match '/decr' => 'counter#decr', via: :post
match '/count' => 'counter#count', via :get
end
class CounterController < ApplicationController
def new
@Counter = Counter.new
end
def get
{ count: @Counter.count }.to_json
end
def incr
@Counter.count += 1
{ count: @Counter.count }.to_json
end
def decr
@Counter.count -= 1
{ count: @Counter.count }.to_json
end
endfunction Counter({count, dispatch}) {
increment = () => dispatch({ type: "INCREMENT" });
decrement = () => dispatch({ type: "DECREMENT" });
return (
<div>
<button onClick={decrement}>-</button>
{count}
<button onClick={increment}>+</button>
</div>
);
}
const mapStateToProps = (state) => ({
count: state.count;
});
export default connect(mapStateToProps)(Counter);Let’s build a counter in elm.
type User
= Regular String String
| Visitor String
thomas = Regular "Thomas" "Kahlua"
kate95 = Visitor "kate95"
greetUser : User -> String
greetUser user =
case user of
Regular name drink ->
"Hi Regular " ++ name ++ "Who's favorite drink is: " ++ drink
Visitor name ->
"Hi Visitor " ++ name ++ "What would you like to order?"JSLand
A breath of fresh air. All states are taken care of by the compiler.
(To me, fighting NULL is the epitome of why I struggled as a programmer. I am not a natural at it, but I wanted very much to be–and I found no use for NULL. I never needed it, but it was always there. I kept pushing it down, painting over it, shutting it up, constantly checking for it–
“Are you NULL?
Are you NULL?
What about you?”
–and sometimes I would deceive myself, that my problems were other things, but then NULL would pop up, I would find that it was the cause–however, NULL is never really the cause. It is someone you always run into in bad situations, someone you never want to see. NULL penetrates all the layers to find you, and can only say, helplessly, “Looks like you’re having a problem.” Endemic to the problem, not the problem, complicit, and might be the problem.)