Skip to content

Instantly share code, notes, and snippets.

View rogerluan's full-sized avatar
🚀

Roger Oba rogerluan

🚀
View GitHub Profile
@rogerluan
rogerluan / DefMode.java
Last active April 9, 2024 19:50
A simple yet extremely effective Robocode robot.
package a20;
import robocode.*;
import java.awt.Color;
/**
* DefMode - A Robocode robot
* @author: Roger Oba
* @date: September 2014
*
* This robot won 2nd place in an internal Robocode tournament. The 1st place had a complex algorithm with over 1k LOC, and most other participants had over 600 LOC ;)
@rogerluan
rogerluan / calculate_cpf_digits.rb
Last active December 27, 2022 20:18
Calculates the digits of a CPF, given the first 9 digits of it.
# Usage: `ruby calculate_cpf_digits.rb "123456789"`
cpf = ARGV[0]
cpf_array = cpf.split("").map(&:to_i)
def calculate_digit(base_array:)
# Constants
divisor = 11 # Constant, fixed number
weighted_array = [10, 9, 8, 7, 6, 5, 4, 3, 2] # Digits, starts with 10 and decrements by 1
# Algorithm
@rogerluan
rogerluan / remove_all_targets_except.rb
Last active August 25, 2022 22:27
Xcode doesn’t gather code coverage from packages when set to “some targets”. This script fixes this. Also see https://forums.swift.org/t/xcode-doesnt-gather-code-coverage-from-packages-when-set-to-some-targets/37220
# 1. Configure your Swift Package scheme to collect code coverage for "all targets" - NOT "some targets"
# 2. Test your Swift Package using either `xcodebuild test`, which should generate a `.xcresult` file.
# 3. Convert that `.xcresult` file into a `.json` file using:
# ```sh
# $ xcrun xccov view --report --json #{path_to_xcresult_file} > #{xccov_json_report}
# ```
# 4. Lastly, strip out the targets that you're not interested using this method.
#
# Note 1: This code considers you only want to keep 1 of the targets.
# Note 2: The resulting json file will replace/override the existing one without any warning.
@rogerluan
rogerluan / String+Regex.swift
Created July 27, 2020 02:26
Handy Regex utility to find and replace matching groups in Swift.
extension String {
/// Finds matching groups and replace them with a template using an intuitive API.
///
/// This example will go through an input string and replace all occurrences of "MyGreatBrand" with "**MyGreatBrand**".
///
/// let regex = try! NSRegularExpression(pattern: #"(MyGreatBrand)"#) // Matches all occurrences of MyGreatBrand
/// someMarkdownDocument.replaceGroups(matching: regex, with: #"**$1**"#) // Surround all matches with **, formatting as bold text in markdown.
/// print(someMarkdownDocument)
///
/// - Parameters:
@rogerluan
rogerluan / commit-message-guidelines.md
Created December 3, 2021 10:06
Commit Message Guidelines

Commit Message Guidelines

These guidelines are based off of this guide. Some of the key takeaways, plus some other opinionated guidelines:

  1. Clarity is key: a commit should be clear even to a viewer who doesn’t have the full context in which it was made.
  2. Start your commit message with a verb, and use sentence case capitalization.
  3. Use the imperative verb when writing a commit message (see the link above if you don’t know what that means). Example: "Fixed" → "Fix"
  4. Commit things that make sense to be grouped in a single commit. Ask yourself "does it make sense for someone to cherry pick this single commit? Is there something missing in it, or something extra that the person wouldn't need?" - if you do that, you'll end up aiming for 1 commit per fix or feature, which makes the codebase cleaner.
  5. Test what you are fixing before committing. Only commit what you’ve tested and are sure that is good to go. This avoids unnecessary comm
@rogerluan
rogerluan / SceneDelegate.swift
Created July 23, 2021 13:02
SceneDelegate default implementation as of Xcode 12.5. Use this template to migrate apps from iOS versions prior to iOS 13, to iOS 13+
// Copyright © 2021 Roger Oba. All rights reserved.
import UIKit
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
@rogerluan
rogerluan / hns-airdrop.md
Last active May 11, 2021 04:10
Github 15+ Followers HNS Airdrop

Had 15 GitHub followers in Feb 2019? You can get about US$ 2,300.00 (rate of May 11th 2021) worth of crypto for minimal effort

Is this a scam?

Hah, I won't waste much time explaining this so: tl;dr: no, it's not a scam.

There's no such thing as free money, so the money being given out here is actually a function of:

  1. Time
  2. You being probably an active Open Source contributor (after all those years, you're finally being rewarded in cash! Or, sorta cash, haha)
@rogerluan
rogerluan / Optional+Utilities.swift
Created January 26, 2021 16:14
The Optional Value Setter operator assigns a value to the receiving subject, only if the receiver is nil. Analogous to Ruby's ||= operator.
infix operator ??=
extension Swift.Optional {
/// This is the Optional Value Setter operator. It assigns a value to the receiving subject, only if
/// the receiver is `nil`.
/// When the receiver is not `nil`, the value on the right hand side won't be accessed or computed (as
/// it's an `@autoclosure`). If the receiver already has `.some` value, no value will be assigned to it,
/// thus `willSet` and `didSet` won't be called.
///
/// - SeeAlso: the rejected proposal of this feature in Swift Evolution: https://github.com/apple/swift-evolution/blob/master/proposals/0024-optional-value-setter.md
@rogerluan
rogerluan / CountryCodes.swift
Created January 19, 2021 08:44
Quick snippet to get all the Alpha-2 ISO country code as well as they localized display name, using native Swift lib (no imported external enums/manual entries).
var countries: [String:String] = [:]
for code in NSLocale.isoCountryCodes {
let id: String = Locale.identifier(fromComponents: [
NSLocale.Key.countryCode.rawValue : code
])
guard let name = (Locale.current as NSLocale).displayName(forKey: .identifier, value: id) else { continue }
countries[code] = name
}
for (index, countryCode) in countries.keys.sorted(by: { countries[$0]! < countries[$1]! }).enumerated() {
@rogerluan
rogerluan / Flattenable.swift
Created December 17, 2020 19:36
Flattenable conforming entities define flattened, a function that flattens any arbitrary Optional, no matter how deeply nested, down to a single level of optionality.
import XCTest
protocol Flattenable {
func flattened() -> Any?
}
extension Swift.Optional : Flattenable {
/// Flattens any arbitrary Optional, no matter how deeply nested, down to a single level of optionality.
/// - SeeAlso: Really fun discussion going on here: https://forums.swift.org/t/challenge-flattening-nested-optionals/24083
/// - Returns: A single-level Optional.