Skip to content

Instantly share code, notes, and snippets.

View afonsomatos's full-sized avatar
🎯
Focusing

Afonso Matos afonsomatos

🎯
Focusing
  • Lisboa, Portugal
View GitHub Profile
@afonsomatos
afonsomatos / ModulesES6.js
Last active August 29, 2015 14:23
Importing and exporting modules in es6
// --- All ways to import a module (main.js)
import MyDefault from './m1';
import { square, diag } from './m1'
import { square as sq, diag as dg} from './m1'
import * as lib from './m1'
import { default as foo } from './m1'
import Animal from './m1';
import './m1' // loads the module (executes the body)
// but doesn't import anything
import MyDefault, * as lib from './m1'
@afonsomatos
afonsomatos / CollectionsES6.js
Created June 18, 2015 12:22
New Collection data structures and methods
// <---------------> Array methods <------------------->
// Array.from(arrayLike or iterable, mapFunc?, thisArg?)
Array.from("hello", c => c.toUpperCase());
// Array.of(...items)
Array.of(...'hello');
// -- Array prototype methods
// Array.prototype.keys() => Returns iterator of keys
['a', 'b', 'c'].keys(); // 0.. 1.. 2..
// Array.prototype.values() => Returns iterator of values
['a', 'b', 'c'].values(); // 'a'.. 'b'.. 'c'..
@afonsomatos
afonsomatos / sum-arr.js
Last active August 29, 2015 14:24
Sum of big integer strings
/*!
* Big Integer Sum Algorithm
*
*
* Result sum( '5' * 10^7, '8' * 10^7)
* real 0m2.995s
* user 0m2.928s
* sys 0m0.060s
*/
@afonsomatos
afonsomatos / quiz.sh
Last active August 29, 2015 14:24
Quiz in bash
#!/bin/bash
# Try to guess number
# Function that generates numbers based on range
function random {
num=$(($RANDOM%$1+1))
echo $num
}
function noargs {
@afonsomatos
afonsomatos / challenge.js
Last active August 29, 2015 14:24
Benjamin's challenge
"use strict";
let x = 101;
// a^2 + b^2 + c^2 + d^2 = 101
// Loop a
for (let a = 0; a < x; ++a) {
// Loop b
for (let b = 0; b < a; ++b) {
// Loop c
@afonsomatos
afonsomatos / syllabes.hs
Created July 11, 2015 19:58
words syllabes
-- Format src.txt so that it gives us a list of words and correspondig syllabes
-- divided by 1 space
import System.IO
import System.Directory
import System.Environment
import Data.List
main = do
-- Open input and output files
function getNextChar (char) {
if (char == 'z') return 'A';
if (char == 'Z') return 'a';
return String.fromCharCode(char.charCodeAt(0) + 1);
}
function getNextString (str) {
@afonsomatos
afonsomatos / FirstSOChatMessage.hs
Created August 22, 2015 17:36
Get first stackoverflow chat message
module Main where
import Network.HTTP (simpleHTTP, getRequest, getResponseBody)
import Text.HTML.TagSoup
import Data.List (isInfixOf, isPrefixOf)
type Link = String
type UserID = String
hostname = "http://chat.stackoverflow.com"
@afonsomatos
afonsomatos / DirTree.hs
Created August 28, 2015 00:33
Get files recursively inside directories
module System.Directory.Tree where
import System.Directory ( doesFileExist, getDirectoryContents )
data DirTree = Directory String [DirTree]
| File String
deriving (Show, Eq)
getDirTree :: FilePath -> IO [DirTree]
getDirTree path = getDirectoryContents path
@afonsomatos
afonsomatos / OverloadedStrings.hs
Created September 8, 2015 16:36
OverloadedStrings example
{-# LANGUAGE OverloadedStrings #-}
import GHC.Exts ( IsString(..) )
newtype Vowels = Vowels String deriving Show
instance IsString Vowels where
fromString = Vowels . filter (`elem` "aeiou")