Skip to content

Instantly share code, notes, and snippets.

View parambirs's full-sized avatar
👋

Parambir Singh parambirs

👋
View GitHub Profile
@parambirs
parambirs / 0_reuse_code.js
Last active August 29, 2015 14:26
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@parambirs
parambirs / insult.scala
Created September 5, 2015 06:51
Scala insult generator
#!/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)
#!/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 {
{-# 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
{-# 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
-- 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
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
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
@parambirs
parambirs / toArray.js
Last active December 13, 2015 07:06
Convert function arguments to array
function toArray(arrayLikeObject) {
return Array.prototype.slice.call(arrayLikeObject);
}
@parambirs
parambirs / js-closure.js
Created December 13, 2015 08:25
Closure - problem and solution
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