Skip to content

Instantly share code, notes, and snippets.

@cdanyl
cdanyl / go-pocker.go
Created February 14, 2023 09:45
Here is an example algorithm in GoLang for shuffling and dealing poker cards from a deck of 52 cards
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
rand.Seed(time.Now().UnixNano()) // initialisation du générateur de nombres aléatoires
@cdanyl
cdanyl / with-visitor-pattern.ts
Last active December 30, 2019 17:15
Visitor Pattern
/*
* Visitor pattern solves the problem elegantly. Start by modifying our base Fruit class:
*/
interface FruitVisitor {
visit(fruit: Orange): void;
visit(fruit: Apple): void;
visit(fruit: Banana): void;
}
abstract class Fruit {
@cdanyl
cdanyl / config.service.ts
Created April 1, 2019 18:23
Init Angular application startup and loads config
import {Injectable} from '@angular/core';
import {Configuration} from '../../../assets/config/config';
import {HttpClient} from '@angular/common/http';
@Injectable()
export class ConfigService {
private defaultConfig: Configuration;
private runningConfig: Configuration;
@cdanyl
cdanyl / enum-alternative.ts
Last active January 24, 2019 14:21
Displaying enums in UI elements like dropDownList
/**
* Enum Alternatives in Typescript.
* Enums are simple value-type flags that provide very minimal protection from invalid values and no behavior.
* Displaying enums in UI elements like DropDownLists is another challenge if they need to have spaces.
*/
class Role {
public static allRoles = [];
public static Author: Role = new Role(0, 'Author');
public static Editor: Role = new Role(1, 'Editor');
@cdanyl
cdanyl / composition.js
Last active January 12, 2019 17:43
object composition over class inheritance
function makeProductList(productDb) {
return Object.freeze({
addProduct,
})
function addProduct() {
return 1;
}
}
@cdanyl
cdanyl / scratch_12.ts
Created March 9, 2018 09:00
angular http wrapper for REST
import * as R from 'ramda';
import { HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { ApiService } from '@core/api-service/api.service';
import { ApiResponseLegacy } from '@core/api-service/apiResponse.interface';
import { Company } from '@shared/services/company/company.model';
@cdanyl
cdanyl / promises.es6
Last active April 26, 2018 18:52
Resolve promises one after another (i.e. in sequence)
// Resolve promises one after another (i.e. in sequence)
function sequence(tasks, fn) {
return tasks.reduce((promise, task) => promise.then(() => fn(task)), Promise.resolve());
}
function inSequence(tasks) {
return tasks.reduce((p, task) => p.then(task), Promise.resolve())
}
@cdanyl
cdanyl / constructor_&_static_factory.ts
Last active March 11, 2020 08:00
Constructors or Static Factory Methods VS. Polymorphism
// Constructors or Static Factory Methods VS. Polymorphism
/*
* Advantages to using static factory methods instead of constructors
* They have names.
* They can cache.
* They can subtype.
*/
class Color {
private hex: number;
@cdanyl
cdanyl / init.class.ts
Last active February 10, 2018 16:35
Initialize Classes by Using an Object Initializer inTypeScript
class Person {
public name: string = "default";
public address: string = "default";
public age: number = 0;
public constructor(init?: Partial<Person>) {
Object.assign(this, init);
}
}
@cdanyl
cdanyl / class_vs_OLOO.es6
Created October 6, 2017 07:49
Classes and inheritance are a design pattern you can choose, or not choose, in your software architecture. Most developers take for granted that classes are the only (proper) way to organize code, but here we've seen there's another less-commonly talked about pattern that's actually quite powerful: behavior delegation.
/**
* Classes vs. Objects "classes" vs. "behavior delegation (OLOO-style)"
*/
/**
* ES6 class
* Example: common base widget behavior, and then child derived classes for specific widget types (like Button)
*/
class Widget {
constructor(width, height) {