Skip to content

Instantly share code, notes, and snippets.

@devpeds
devpeds / Singleton.swift
Created October 19, 2017 15:38
Thread-Safe Singleton Pattern in Swift
/* 1. Class Constant : Lazy initialization is supported. Officially recommanded way */
class Singleton {
static let shared = Singleton()
private init() { } // prevent creating another instances.
}
/* 2. Nested Struct : Workaround for the lack of static class constants in Swift 1.1 and earlier. still useful
in functions, where static constants and varialbles cannot be used. */
@devpeds
devpeds / Singleton.swift
Created October 19, 2017 15:38
Thread-Safe Singleton Pattern in Swift
/* 1. Class Constant : Lazy initialization is supported. Officially recommanded way */
class Singleton {
static let shared = Singleton()
private init() { } // prevent creating another instances.
}
/* 2. Nested Struct : Workaround for the lack of static class constants in Swift 1.1 and earlier. still useful
in functions, where static constants and varialbles cannot be used. */
@devpeds
devpeds / binary-tree.js
Created May 30, 2017 18:42
JavaScript Data Structure - Binary Tree
// Node
var Node = function(key) {
this.key = key;
this.parent = null;
this.right = null;
this.left = null;
}
Node.prototype = {
getKey: function() { return this.key; }
@devpeds
devpeds / circular-linked-list.js
Created May 29, 2017 23:33
JavaScript Data Structure - Circular Linked List
// Node
var Node = function(key) {
this.key = key;
this.next = null;
};
Node.prototype = {
getKey: function() { return this.key; }
,
getNext: function() { return this.next; }
@devpeds
devpeds / doubly-linked-list.js
Created May 29, 2017 23:07
JavaScript Data Structure
// Node
var Node = function(key) {
this.key = key;
this.next = null;
this.prev = null;
};
Node.prototype = {
getKey: function() { return this.key; }
,
@devpeds
devpeds / singly-linked-list.js
Last active May 29, 2017 23:34
JavaScript Data Structure - Singly Linked List
// Node
var Node = function(key) {
this.key = key;
this.next = null;
};
Node.prototype = {
getKey: function() { return this.key; }
,
getNext: function() { return this.next; }
@devpeds
devpeds / deque.js
Created May 26, 2017 14:52
JavaScript Data Structure - Deque
var Deque = function() {
this.arr = [];
};
Deque.prototype.size = function() {
return this.arr.length;
};
Deque.prototype.empty = function() {
return this.size() === 0;
@devpeds
devpeds / queue.js
Last active May 26, 2017 14:45
JavaScript Data Structure - Queue
var Queue = function() {
this.data = [];
};
Queue.prototype.size = function() {
return this.data.length;
}
Queue.prototype.empty = function() {
return this.size() === 0;
@devpeds
devpeds / stack.js
Last active May 26, 2017 13:59
JavaScript Data Structure - Stack
var Stack = function() {
this.data = [];
};
Stack.prototype.size = function() {
return this.data.length;
};
Stack.prototype.empty = function() {
return this.size() === 0;