Beginner questions about signals

95 views
Skip to first unread message

Andy Nicholson

unread,
Jul 10, 2015, 10:10:04 PM7/10/15
to elm-d...@googlegroups.com
Hi everyone,

I'm from a Ruby background, so it's fun learning to deal with static types instead of Ruby's fluidity :)

I'm building a hangman game as my first foray into Elm, and I can render the state of a game into a browser window.



I want to be able to register keystrokes as guesses, so they can update the model Signal. I know that Keyboard.keyPresses is the signal I want to track, I just don't know how to "bind" that value change into my model.

Can someone help, please?

Thanks,

Andy Nicholson
@andrewdotnich

Max Goldstein

unread,
Jul 10, 2015, 11:27:35 PM7/10/15
to elm-d...@googlegroups.com
You're really close!

At the top of the file add

import Keyboard
import Char

At some point, define

keypresses = Signal.map (Char.fromCode >> Guess) Keyboard.presses


The >> symbol is function composition: it takes each press, converts it from a charCode to a Char, and then wraps it in the Guess tag. Then in model replace actions.signal with (Signal.merge keypresses actions.signal). That should do it.


(It's possible to send the keypresses to the mailbox instead of merging, but that requires a port, which should be avoided if it's ease to do so like here.)


This is a great first foray and I hope you stick around.

Andy Nicholson

unread,
Jul 31, 2015, 12:38:32 AM7/31/15
to Elm Discuss, maxgol...@gmail.com
Thanks Max!

I'm so nearly close to calling this example "finished" - but with one hiccup.

I have a module with a list of words, which I want to randomly pick a word from. I created a port, and tried to inject a timestamp into my App as a random seed. But the code as it stands causes a stack overflow.

What have I done wrong?

Corey Trampe

unread,
Jul 31, 2015, 8:54:06 AM7/31/15
to elm-d...@googlegroups.com
I left a note on the last commit, in the index.html.

Initialize a port:
    var app = Elm.embed( Elm.Main, stageDiv, {myPort: 42} );

Send a value into a port:
    app.ports.myPort.send(138);



--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages