Skip to content

Instantly share code, notes, and snippets.

View leopic's full-sized avatar

Leo Picado leopic

View GitHub Profile
@leopic
leopic / dictionary.swift
Created February 6, 2021 18:33
Recursively merging dictionaries
static func mergeBlocks(base: [String: Any], replacement: [String: Any]) -> [String: Any] {
var result = base.merging(replacement) { $1 }
for key in base.keys {
guard let val = base[key] else { continue }
guard let json = val as? [String: Any],
let replacement = replacement[key] as? [String: Any] else {
continue
}
import Foundation
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
// Taken from https://www.avanderlee.com/swift/asynchronous-operations/
class AsyncOperation: Operation {
private let lockQueue = DispatchQueue(label: "com.leopicado.asyncop", attributes: .concurrent)
override var isAsynchronous: Bool {
protocol MyProtocol {
func requiredMethod() -> Void
func optionalMethod() -> Void
}
extension MyProtocol {
func optionalMethod() -> Void {} // Default implementation
}
struct AStructThatConformsToMyProtocol:MyProtocol {
@leopic
leopic / boyespeak-playground.swift
Created May 1, 2020 04:05
Having fun with Swift
import Foundation
import UIKit
import Combine
var texto = CurrentValueSubject<String, Never>("hola buen chico")
let sub = texto
.map { $0.boyeSpeakify }
.handleEvents(receiveOutput: { UIPasteboard.general.string = $0 })
.sink { print($0) }
enum HttpServiceError: Error {
case uno
case dos
}
struct HttpService {
func call() -> throws {}
}
enum FirmwareServiceError: Error {
'use strict';
const lib = require('../../lib/index');
describe('Basic functionality', function () {
it('displays the help banner when no params are given', function () {
lib({})
.then(result => expect(result).toContain('Usage: makeappicon --base-icon ICON_PATH'))
.catch();
});
'use strict';
// Module dependencies
var _ = require('lodash'),
fs = require('fs'),
gm = require('gm'),
path = require('path');
// Global variables
var EXECUTION_PATH = process.cwd() + path.sep,
@leopic
leopic / fakeAsync.patch
Created July 19, 2017 16:52
src/sample.spec.ts
────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
--- a/src/sample.spec.ts
+++ b/src/sample.spec.ts
────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
@@ -1,5 +1,6 @@
import { TestBed, inject, async } from '@angular/core/testing';
import { TestBed, inject, async, fakeAsync, tick } from '@angular/core/testing';
import { SampleModule, SampleService } from "./";
describe('SampleTest', () => {
@leopic
leopic / explain.swift
Created December 1, 2016 22:44
A couple of Swift 3 things that I got confused by
import Foundation
// @escape VS @no-escaping
class Uno {
func closure(completion: () -> Void) {
// Non-escaping
completion()
// Escaping
// DispatchQueue.main.asyncAfter(deadline: .now() + 5, execute: {
@leopic
leopic / ForegroundUtils.java
Created April 13, 2016 22:11 — forked from PiXeL16/ForegroundUtils.java
Singleton Util that share the status of the App when it goes to background or foreground
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;