Skip to content

Instantly share code, notes, and snippets.

View nsomar's full-sized avatar

Omar Abdelhafith nsomar

View GitHub Profile
> Utilities.write(content, file) |> IO.inspect
#PID<0.256.0>
defmodule Utilities do
def write(content, file) do
spawn(fn ->
# write stuff to disk
# takes long time
end)
end
end
defmodule Utilities do
def write(content, file) do
# write stuff to disk
# takes long time
end
end
defmodule Website do
def index(conn, content) do
# some calculations
# Creating
Agent.start_link(fn -> [0] end, name: Storage)
# Update
Agent.update(Storage, fn state -> state ++ [1] end)
Agent.update(Storage, fn state -> state ++ [2] end)
# Retreive
Agent.get(Storage, fn state -> state end) |> IO.inspect
> Storage.start([0])
> Storage.put(1)
> Storage.put(2)
> Storage.get() |> IO.inspect
[0, 1, 2]
defmodule Storage do
def start(initial) do
process = spawn_link(fn -> execute(initial) end)
Process.register(process, Storage)
process
end
def execute(state) do
receive do
@nsomar
nsomar / sign.sh
Created January 25, 2017 10:51
Sign an ipa on travis
#!/bin/sh
# Create a custom keychain
security create-keychain -p travis ios-build.keychain
# Make the custom keychain default, so xcodebuild will use it for signing
security default-keychain -s ios-build.keychain
# Unlock the keychain
security unlock-keychain -p travis ios-build.keychain
@nsomar
nsomar / counter-class.swift
Created September 24, 2016 00:13
Implementing a counter as a class
// Impelentation Counter as a class
class Counter {
private var x: Int
init(number: Int) {
x = number
}
func setNumber(_ number: Int) {
x = number
@nsomar
nsomar / counter-function.swift
Last active September 24, 2016 00:14
Implementing a counter as a function
// Impelentation Counter as a Function
typealias Mutators = () -> ()
typealias Getter = () -> Int
typealias Setter = (Int) -> ()
func Counter(number initial: Int) -> (getNumber: Getter, setNumber: Setter, increment: Mutators, decrement: Mutators) {
var v = initial
let getNumber = { return v }
import Foundation
func cleanRegex(pattern: String) -> String {
let res = pattern.characters.enumerate().reduce(([], [])) { (acc, enumerator) -> ([Int], [Int]) in
if enumerator.element == "{" {
return (acc.0 + [enumerator.index], acc.1)
}
if enumerator.element == "}" {