This file contains 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
// Use Gists to store code you would like to remember later on | |
console.log(window); // log the "window" object to the console |
This file contains 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 scala | |
import scala.collection.immutable.IndexedSeq | |
val alphabets = 'A' to 'Z' | |
val insult = if(args.length > 0) args(0).toUpperCase else "STUPID" | |
val heading = alphabets filterNot (insult contains(_)) | |
println(getInsult) |
This file contains 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 scala | |
import scala.collection.immutable.IndexedSeq | |
import scala.io.StdIn | |
val version = "Scala Insult Generator v0.1" | |
val cmdArg = if(args.length > 0) Some(args(0).toUpperCase) else None | |
val result = cmdArg match { |
This file contains 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
{-# LANGUAGE OverloadedStrings #-} | |
module Article where | |
import Data.Text.Lazy | |
import Data.Text.Lazy.Encoding | |
import Data.Aeson | |
import Control.Applicative | |
-- Define the Article constructor | |
-- e.g. Article 12 "some title" "some body text" | |
data Article = Article Integer Text Text -- id title bodyText |
This file contains 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
{-# LANGUAGE OverloadedStrings #-} | |
import Web.Scotty | |
import Network.HTTP.Types | |
main = scotty 3000 $ do | |
get "/" $ do -- handle GET request on "/" URL | |
text "This was a GET request!" -- send 'text/plain' response | |
delete "/" $ do | |
html "This was a DELETE request!" -- send 'text/html' response | |
post "/" $ do |
This file contains 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
-- set a header: | |
post "/set-headers" $ do | |
status status302 -- Respond with HTTP 302 status code | |
setHeader "Location" "http://www.google.com.au" | |
-- named parameters: | |
get "/askfor/:word" $ do | |
w <- param "word" | |
html $ mconcat ["<h1>You asked for ", w, ", you got it!</h1>" ] | |
-- unnamed parameters from a query string or a form: | |
post "/submit" $ do -- e.g. http://server.com/submit?name=somename |
This file contains 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
main = scotty 3000 $ do | |
-- get article (json) | |
get "/article" $ do | |
json $ Article 13 "caption" "content" -- Call Article constructor and encode the result as JSON | |
-- post article (json) | |
post "/article" $ do | |
article <- jsonData :: ActionM Article -- Decode body of the POST request as an Article object | |
json article -- Send the encoded object back as JSON |
This file contains 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
FROM haskell:7.10 | |
RUN cabal update | |
# Add .cabal file | |
ADD ./scotty-webapp-example.cabal /opt/server/scotty-webapp-example.cabal | |
# Docker will cache this command as a layer, freeing us up to |
This file contains 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
function toArray(arrayLikeObject) { | |
return Array.prototype.slice.call(arrayLikeObject); | |
} |
This file contains 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
var result = []; | |
for (var i=0; i < 5; i++) { | |
result.push(function () { return i }); // (1) | |
} | |
console.log(result[1]()); // 5 (not 1) | |
console.log(result[3]()); // 5 (not 3) | |
for (var i=0; i < 5; i++) { | |
(function () { | |
var i2 = i; // copy current i |
OlderNewer