Skip to content

Instantly share code, notes, and snippets.

View jlacar's full-sized avatar

Junilu Lacar jlacar

  • Columbus, OH
View GitHub Profile
@jlacar
jlacar / formatStringsKvsJ.md
Last active November 17, 2021 11:56
Formatting strings in Kotlin

This is just one way Kotlin improves on Java.

In Java, you can use String.format(), like so:

  System.out.println("".format("Hello, %s!", "world"));

This works because the static format() method can be called using a String reference which is why we start the expression with an empty string, "". The format string needs to be passed in as the first argument and there needs to be enough subsequent arguments to match all the format placeholders.

In Kotlin, it's much simpler and more object-oriented. You can call format on a String and the string itself will be treated as the format string. You only need to pass in enough arguments to match all the format placeholders.

@jlacar
jlacar / PatternSplitLimitPitfallInKotlin.md
Last active May 21, 2020 19:26
Pattern.split() default limit pitfall in Kotlin

Pattern.split() pitfalls in Kotlin

This is about a little pitfall I stumbled upon recently while trying to use regular expressions in Kotlin to split strings. It is relevant to interoperability between Pattern.split() Kotlin's CharSequence.split().

The Gist

  1. Pattern is part of the Standard Java Library and is documented in the Standard Java API Specifications.
  2. Kotlin's CharSequence, String, and Regex classes have a split() method that is documented in the Kotlin Standard Library documentation. Everything that applies to CharSequence.split() also applies to Regex.split() and String.split().
  3. Pattern.split() uses the limit parameter differently from how CharSequence.split() uses it. Despite having the same default limit of 0, their results can be different when trailing empty strings are involved.
  4. `Pattern.
@jlacar
jlacar / Main.java
Last active April 19, 2020 23:03
Filtering in Java using predicates, lambdas, and enums implementing the Strategy pattern
import java.util.*;
import java.util.function.*;
/**
* Sample program to show use of Predicates and composing Predicates,
* capturing variables in lambdas expressions, and using enums to
* implement the Strategy pattern.
*
* Inspired by this discussion on CodeRanch.com:
* https://coderanch.com/t/729390/engineering/Design-pattern-good-command-pattern
@jlacar
jlacar / MapAssociateByElvis.kt
Created April 7, 2020 04:27
Kotlin - Map.associateBy and Elvis operator
// Example of Map.associateBy() in Kotlin
// Map vowels to their positions in the alphabet
val vowels = "aeiou".toCharArray() // ['a', 'e', 'i', 'o', 'u']
// Map<Char, Int> = {a=1, e=5, i=9, o=15, u=21}
val positions = vowels.associateBy({ it }, { it.toInt() - 'a'.toInt() + 1 })
val letter = readLine()!!
println(positions[letter.first().toLowerCase()] ?: 0) // default == 0 if not a vowel
@jlacar
jlacar / google-foobar-gearingup-solved.md
Last active April 9, 2023 19:53
Math for solution to Google foo.bar challenge - Gearing Up for Destruction

This is about the Google foo.bar Gearing Up for Destruction challenge - 2nd of 2 challenges on Level 2. It was pretty interesting and I got through most of it without looking for answers. I did get stuck on 2 failing tests so I finally caved. I didn't copy my solution though, just got an insight from the solution that @CBarraford posted here: https://gist.github.com/1lann/be45311db1bd8cbbe6650b0a3e9d1977, which was a more brute-force trial-and-error solution.

Here's the math I worked out for this problem:

Given: 
   n pegs, numbered from 1 to n

Let:
   d(n) - the distance between peg[n] and peg[n+1] 

Vim Cheat Sheet

Note: Work-In-Progress - I will be updating this as I learn my way around vi. I'll add new entries as and when I find that I'm using them frequently

Working with files

:e .           - edit file, show list to choose from

:e filename    - edit a specific file

:q! - quit without saving
@jlacar
jlacar / sweet-spock.md
Last active March 10, 2016 05:45
Add some syntactic sugar to make Spock sweet!

Use label comments to make Spock specs more conversational

Spock and Geb (pronounced /jeb/), are relatively new Groovy-based frameworks for writing Behavior-Driven Development (BDD) style test specifications. They allow you to write very concise and expressive test code.

The example code below is from gebish.org:

class LoginSpec extends GebSpec {
    def "login to admin section"() {
 given:
@jlacar
jlacar / format-xml-vi.md
Last active May 25, 2023 18:52
Format XML in vi using xmllint

Formatting XML in vi

Using xmllint

You can format XML in vi using xmllint

:%!xmllint "%" --format

Default indentation is 2 spaces. You can change that by assigning a value to the XMLLINT_INDENT environment variable. To do that without