Skip to content

Instantly share code, notes, and snippets.

View nsomar's full-sized avatar

Omar Abdelhafith nsomar

View GitHub Profile
@nsomar
nsomar / Lazy collection implementation
Created August 6, 2014 23:46
Implementation of lazy collection in swift
typealias Closure = (Int) -> (Bool)
class MyFilterArrayView: SequenceType, GeneratorType {
var _array: [Int]?
var _lazyFilter: MyFilterArrayView?
var _closure: Closure
var _currentIndex: Int = 0
var array: [Int] {
get {
1.sumToArray([1,2,3])
"a".sumToArray(["1","2","3"])
import Queue
a = [1, [2], [3], [4,5]]
q = Queue.Queue()
res = []
def copy_arr_to_q(arr, q):
for item in arr:
@nsomar
nsomar / fix.sh
Created August 5, 2015 20:19
Mac OSX El Capitan beta 6 (Public beta pb 4) Xcode 6 crash fix
# Xcode 6
cd /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer
cd SDKs/iPhoneSimulator.sdk/usr/lib/
sudo mv dyld_sim dyld_sim_orig
# Xcode beta
cd /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer
cd SDKs/iPhoneSimulator.sdk/usr/lib/
sudo mv dyld_sim dyld_sim_orig
96: FBProfileSetEventsCalendarSubscriptionStatusMutationOptimisticPayloadFactoryProtocol-Protocol.h
95: FBGroupUpdateRequestToJoinSubscriptionLevelMutationOptimisticPayloadFactoryProtocol-Protocol.h
94: FBEventUpdateNotificationSubscriptionLevelMutationOptimisticPayloadFactoryProtocol-Protocol.h
93: FBReactionUnitUserSettingsDisableUnitTypeMutationOptimisticPayloadFactoryProtocol-Protocol.h
93: FBMemReactionAcornSportsContentSettingsSetShouldNotPushNotificationsResponsePayloadBuilder.h
92: FBReactionUnitUserSettingsEnableUnitTypeMutationOptimisticPayloadFactoryProtocol-Protocol.h
91: FBProfileUpdateSecondarySubscribeStatusMutationOptimisticPayloadFactoryProtocol-Protocol.h
91: FBViewerNotificationsUpdateAllSeenStateMutationOptimisticPayloadFactoryProtocol-Protocol.h
90: FBMemReactionAcornSportsContentSettingsSetShouldPushNotificationsResponsePayloadBuilder.h
89: FBMemReactionAcornTvContentSettingsSetShouldNotPushNotificationsResponsePayloadBuilder.h
This file has been truncated, but you can view the full file.
@nsomar
nsomar / with.exs
Created April 4, 2016 21:40
Handling errors with `with`
res =
with {:ok} <- validate_request(request),
{:ok, user} <- get_user(request),
{:ok} <- update_db(user),
{:ok} <- send_email(user) do
return_http_message
end
case res do
{:error, x} -> IO.inspect("Error: " <> x)
@nsomar
nsomar / substring.swift
Created June 3, 2016 08:19
Inset a string one character from start and end
let string = "This is the string"
let start = string.index(string.startIndex, offsetBy: 1)
let end = string.index(string.endIndex, offsetBy: -1)
string[start..<end]
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 == "}" {
@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 }