Skip to content

Instantly share code, notes, and snippets.

View alexwal's full-sized avatar
❤️
🧡💛💚💙💜🖤

Alexander Walczak alexwal

❤️
🧡💛💚💙💜🖤
  • New York, NY
View GitHub Profile
@jverkoey
jverkoey / UIFont+CustomizedDynamicType.m
Created April 14, 2021 01:07
Dynamic Type system fonts with custom point sizes, weight, and italics
static const CGFloat kFontWeightEpsilon = FLT_EPSILON;
@implementation UIFont (CustomizedDynamicType)
+ (nonnull UIFont *)preferredFontWithDefaultSize:(CGFloat)size
textStyle:(nonnull UIFontTextStyle)textStyle {
return [self preferredFontWithDefaultSize:size
textStyle:textStyle
fontWeight:UIFontWeightRegular
italic:NO];
/// A fun Swift 5 way to concatenate a collection of String? elements, for example a [String?].
extension String.StringInterpolation {
mutating func appendInterpolation(_ array: [String?]) {
appendLiteral(array.compactMap { $0 }.joined(separator: ", "))
}
}
let array: [String?] = [nil, nil, "hello", nil, "world", nil, "abc", nil, nil, nil, nil]
print(array) // prints "[nil, nil, Optional("hello"), nil, Optional("world"), nil, Optional("abc"), nil, nil, nil, nil]"
print("\(array)") // prints "hello, world, abc"
//
// ViewController.swift
// DiffableTest
//
// Created by Paul Wilkinson on 28/9/20.
//
import UIKit
class ViewController: UIViewController {
@dry1lud
dry1lud / combine-retry.md
Last active November 27, 2023 10:07
Retry operation in Swift/Combine.

There is a function .retry() in Combine that helps to retry a request. Although it is not enough just to call retry() to achieve retring logic. The retry() function does not do another request but it re-subscribes only to a publisher. To make another request the tryCatch() might be used. In the code below if the first call fails there are three attempts to retry (retry(3)):

import UIKit
import Combine
import PlaygroundSupport

enum CustomNetworkingError: Error {
    case invalidServerResponse
}
// Safely Modifying The View State (SwiftUI)
// https://swiftui-lab.com
// https://swiftui-lab.com/state-changes
import SwiftUI
struct CustomView: View {
var body: some View {
NavigationView {
@fmtonakai
fmtonakai / AttributedString.swift
Last active August 6, 2023 21:27
AttributedString with String Interpolation
//
// AttributedString.swift
//
// Created by fm.tonakai on 2019/04/08.
//
import UIKit
public struct AttributedString: ExpressibleByStringLiteral, ExpressibleByStringInterpolation, CustomStringConvertible {
public struct StringInterpolation: StringInterpolationProtocol {
@alexwal
alexwal / tensorflow-graph-error-handling.py
Last active December 15, 2020 12:03
Example of how to handle errors in a tf.data.Dataset input pipeline
import tensorflow as tf
def create_bad_dataset(create_batches=True):
dataset = tf.data.Dataset.from_tensor_slices([1., 2., 0., 4., 8., 16.])
# Computing `tf.check_numerics(1. / 0.)` will raise an InvalidArgumentError.
if create_batches:
# Demonstrates that error handling works with map_and_batch
dataset = dataset.apply(tf.contrib.data.map_and_batch(
map_func=lambda x: tf.check_numerics(1. / x, 'error'), batch_size=2))
@alexwal
alexwal / 4Sum.py
Last active September 11, 2018 20:11
Leet Code 4Sum Solution: https://leetcode.com/problems/4sum/
class Solution:
"""
Solution to Leet Code problem 4Sum: https://leetcode.com/problems/4sum/
Runtime: 100 ms beats 93.4% of python3 submissions
"""
def fourSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[List[int]]
# Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License").
# You may not use this file except in compliance with the License.
# A copy of the License is located at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# or in the "license" file accompanying this file. This file is distributed
# on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either