hash map – Is there a more idiomatic way to process these strings?

New Scala dev here:

Is there a more idiomatic or efficient way to accomplish this in Scala?

Given a list of the following strings, I need to obtain the unique ‘main parts’ once any parts including and after a “-” have been removed.

The output should be a list of sorted strings. Also note both “;” and “,” have been used as separators.


val data: List(String) = List(
  "W22; O21; B112-WX00BK; G211; B112-WI00BK; G11",
  "W22; K122l; B112-WI00BK; O21; B112-WX00BK; G211",
  "W21, V32",
  "W21, N722",
  "S133-VU3150; S133-SU3150; R22-VK3150; R123-VH3"

Desired Output:

    B112 G11 G211 O21 W22,
    B112 G211 K122l O21 W22,
    V32 W21,
    N722 W21,
    R123 R22 S133

My solution:

def process(input: String): String =
    input.split(" ").map(word => {
        if (word contains "-")
        else word
        .replace(";", "").replace(",","")}).toSet.toList.sorted.mkString(" ")

val result: List(String) = data.map(process(_))

kotlin – Is there a more idiomatic way to iterate ArchiveInputStream or other list type structures

fun ArchiveInputStream.forEachEntry(doThis: (ArchiveEntry) -> Unit) {
    var e: ArchiveEntry?
    while (this.nextEntry.also { e = it } != null) {
        e?.let { doThis(it) }

I’m wondering if there is a better way to call next on an item until it is null and then use that in supplied function. It feels a bit weird to check if it’s null and then to still have to use e?.let instead of just doThis(e)

c# – What is the idiomatic way to split code between separate files in Rust?

Coming from the world of C#, where, despite sharing namespaces, it’s quite common for every class to have its own file, I find that Rust codebases seem to have a complete different sort of philosophy (and perhaps less organization) in the sense that lots of structs, enums, traits, etc. are combined in the same files, which seems to make it more difficult to track down code at times. That’s further compounded when you see a struct’s implementations spanned across multiple files.

This question is not a technical one. I understand what you can do in the module system, but I’d like to see some comments about the idiomatic, perhaps philosophical idea behind the way Rust developers tend to manage their code across different files.

What’s idiomatic Rust for recursion and memoization?

use std::{cmp::max_by_key, collections::HashMap};

fn longest_common_subsequence(s1: Vec<u8>, s2: Vec<u8>) -> Vec<u8> {
    struct Helper {
        s1: Vec<u8>,
        s2: Vec<u8>,
        cache: HashMap<(usize, usize), Vec<u8>>,
    impl Helper {
        fn helper(&mut self, i: usize, j: usize) -> Vec<u8> {
            if self.cache.contains_key(&(i, j)) {
                return self.cache(&(i, j)).clone();
            let value = {
                if i >= self.s1.len() || j >= self.s2.len() {
                } else if self.s1(i) == self.s2(j) {
                    let mut tmp = vec!(self.s1(i));
                    tmp.append(&mut self.helper(i + 1, j + 1));
                } else {
                    max_by_key(self.helper(i + 1, j), self.helper(i, j + 1), |s| s.len())
            self.cache.insert((i, j), value.clone());
            return value;
    return Helper {
        cache: HashMap::new(),
    .helper(0, 0);

fn main() {

I’m playing around with Rust (only a few days into it) for a toy project in which I had to find the canonical representation for a given term — I figured longest common subsequence of first few Google search results (titles) might do the trick, so I started implementing it and this is what I have (from a mental translation of how I’d do it in Python).

Among other things, it looks.. ugly to me. What can I do better (shorter / more readable e.t.c.)?

haskell – What is the idiomatic way to represent algebraic datatype constructors in Scheme (R6RS)?

I’m wondering what is the best way to translate Haskell-like datatypes to Scheme. My current plan is to represent constructors with vectors, with the first element being a label representing the variant. So, for example, the following Haskell program:

data Bits       = O Bits | I Bits | E deriving Show
data Nat        = S Nat  | Z          deriving Show
inc (O pred)    = I pred
inc (I pred)    = O (inc pred)
inc E           = E
dup (S pred)    = let (x,y) = dup pred in (S x, S y)
dup Z           = (Z, Z)
bus Z        bs = inc bs
bus (S pred) bs = let (x,y) = (pred,pred) in (bus pred (bus pred bs))
o32             = (O (O (O (O (O (O (O (O (O (O (O (O (O (O (O (O (O (O (O (O (O (O (O (O (O (O (O (O (O (O (O (O E))))))))))))))))))))))))))))))))
n26             = (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S Z))))))))))))))))))))))))))
main            = print (bus n26 o32)

Would be translated as:

(define (O pred)   (vector 'O pred))
(define (I pred)   (vector 'I pred))
(define E          (vector 'E))
(define (S pred)   (vector 'S pred))
(define Z          (vector 'Z))
(define (Inc bits) (case (vector-ref bits 0) ('O (I (vector-ref bits 1))) ('I (O (Inc (vector-ref bits 1)))) ('E E)))
(define (Dup val)  (case (vector-ref val 0) ('S (let ((xy (Dup (vector-ref val 1)))) (cons (S (car xy)) (S (cdr xy))))) ('Z (cons Z Z))))
(define (Bus n bs) (case (vector-ref n 0) ('Z (Inc bs)) ('S (let ((xy (Dup (vector-ref n 1)))) (Bus (car xy) (Bus (cdr xy) bs))))))
(define O32        (O (O (O (O (O (O (O (O (O (O (O (O (O (O (O (O (O (O (O (O (O (O (O (O (O (O (O (O (O (O (O (O E)))))))))))))))))))))))))))))))))
(define N26        (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S Z)))))))))))))))))))))))))))
(display (Bus N26 O32))

To my surprise, this actually performs quite well (Scheme is faster than Haskell here). But I wonder if this is the best way to do it? Is this reasonable, or is there some more “idiomatic” translation, which would be expected to perform better?

Idiomatic Go and MySQL models

I’m new to Go and I have a question regarding dependencies that I’m trying to pass down to my models. While my code is working perfectly, I’m still not sure if my way is idiomatic or it’s fine and up to my preferences.

I started writing simple REST API using core HTTP package, Gorilla mux and official mysql package.

My project structure is as follows:

In ~/go/src/github.com/<myhandle>/<project>
(project root dir)
   - (db)
      - database.go
   - (models)
      - (user)
         - user.go
   - (controllers)
      - (user)
         - user.go
   - main.go

I’ve initialized database connection in main.go and I pass it down to controllers.


ctrs := router.InitializeRouter(mux, ctr_user.Routes(db), ...)
log.Fatal(http.ListenAndServe(":3000", ctrs))


This is just example, it’s pretty static and I know I shouldn’t be using GET here.

type Ctx struct {
    Subrouter *mux.Router
    DB        *sql.DB

func (ctx *Ctx) Register(w http.ResponseWriter, r *http.Request) {
    user := users.UserRegister{Username: "newuser", Email: "new@example.com", Password: "blah", DB: ctx.DB}

func Routes(db *sql.DB) *ctx.Controller {
    mux := mux.NewRouter()
    ctr := &Ctx{Subrouter: mux, DB: db}
    ctr.Subrouter.HandleFunc("/", ctr.Register).Methods("GET")
    return &ctx.Controller{Path: "/users", Controller: mux}


type UserRegister struct {
    Username string `json:"username"`
    Email    string `json:"email"`
    Password string `json:"password"`
    DB       *sql.DB
func (u *UserRegister) New() {
    res, err := u.DB.Exec(`INSERT INTO users(username,email,hash) VALUES(?,?,?)`, u.Username, u.Email, u.Password)
    if err != nil {
    id, _ := res.LastInsertId()
    fmt.Println("User added with ID: ", id)

So the code works, but I have no idea if this is the way it should be done. Primarily the way DB instance was handed down to model from controller.

I’ve seen examples on github where DB is passed down as parameter, like this:

func (u *UserRegister) New(db *sql.DB) {
     res, err := db.Exec(...)

So am I doing this wrong or it’s up to my preference?

Idiomatic Golang Unit Testing – Software Engineering Stack Exchange

Currently I have some code which is structured like this:

type Service struct {
    // some dependencies

func (s *Service) FindStuff(ctx Context) { // this signature cannot be changed
    // some logic...
    isNew := s.isNewUser(ctx)
    if isNew {
        // call new flow
    } else {
        // call old flow

func (s *Service) isNewUser(ctx Context) bool {
    value := apiGet("some-endpoint")
    // some logic...
    for {
        for {
            if  { return true }
            if  { return false }
            if  { return false }
    return true
  • The existing unit tests call FindStuff which calls isNewUser, the call apiGet("some-endpoint") is mocked.
  • The isNewUser method is used by multiple methods on the Service class.

In a Java world you could create a new class which has a method isNewUser you could then pass a mock object of that class as a dependency to Service and mock the call to isNewUser to return either true of false and allowing everything to be tested in isolation.

What is the most idiomatic Go way of testing this?

algebraic manipulation – Idiomatic way to express a polynomial in terms of a shifted variable

I have a polynomial of known degree, $f(x) = sum_{n = 0}^N a_n x^n$ and I’ d like to express it in terms of a shifted variable $x – x_ 0$, so that $f(x) = sum_{n = 0}^N b_n (x – x_ 0)^n$.

I can do this by hand on paper, and I have a round about way of achieving the same result in Mathematica, but I’ m pretty sure there has to be a standard name for the procedure I’ m following when doing it manually. The round about way I currently use exploits the Taylor series, and goes like this:

Normal(Series(1 + x + x^6, {x, x0, 6}))
Simplify(Normal(Series(1 + x + x^6, {x, x0, 6}))) == 1 + x + x^6

Is there a more compact way of achieving this?

.net – What would be the idiomatic F# way to write optional parameters?

In the context of a library to generate random credit card, it would be nice to let the user the possibility to set some optional options on some cards. Lets take Visa. Two lengths are availables: 13 or 16 digits.

In C# a common implementation would be:

enum VisaLengthOptions

public static string GenerateVisa(VisaLengthOptions length = VisaLengthOptions.Sixteen) {
    if (length == VisaLengthOptions.Thirteen) return "4000000000001";
    if (length == VisaLengthOptions.Sixteen) return "4000000000000001";
    return "";

Since F# does not support optional parameter (for currying and simplicity reason (note that it could be done since ocaml did it)). What would be the most idiomatic way to write this in F#?

Here is a guess:

type VisaLengthOptions =
    | Thirteen = 0
    | Sixteen = 1

let constructVisa length =
    match length with
    | VisaLengthOptions.Thirteen -> "4000000000001"
    | VisaLengthOptions.Sixteen -> "4000000000000001"
    | _ -> ""

let generateVisa () = constructVisa VisaLengthOptions.Sixteen

So there are no optional parameter and have two functions cant have the same name even with differents signatures. What wrong with this snippet?

We want our library to have a Truly Discoverable API. So we would like to avoid having two differents name for basically the same logic. Should we follow the map and map2 and goes generateVisa and generateVisa2? or like map and mapi and goes generateVisa and generateVisaL? Is there a one true F# way?

A good example in the .net world is Split.

java – Searching for an idiomatic Rust implementation of Minimum Edit Distance (LeetCode #72)

I am solving LeetCode Dynamic Programming challenges (this one is #72, at https://leetcode.com/problems/edit-distance).

Here below, I’ve implemented a Rust solution for the minimum edit distance between two strings problem

use std::collections::HashMap;
use std::cmp::min;

impl Solution {
    pub fn min_distance(word1: String, word2: String) -> i32 {
        let mut D: HashMap<(i32, i32), i32> = HashMap::new();
        let m = word1.len() as i32;
        let n = word2.len() as i32;

        let w1: Vec<char> = word1.chars().collect();
        let w2: Vec<char> = word2.chars().collect();

        for i in 0..=m {
            D.insert((i, 0), i);
        for j in 0..=n {
            D.insert((0, j), j);

        for i in 1..=m {
            for j in 1..=n {
                if w1((i-1) as usize) == w2((j-1) as usize) { 
                    D.insert((i, j), *D.get(&(i-1, j-1)).unwrap());
                } else {
                    let p = *D.get(&(i - 1, j - 1)).unwrap();
                    let q = *D.get(&(i - 1, j)).unwrap();
                    let r = *D.get(&(i, j - 1)).unwrap();

                    D.insert((i, j), 1 + min(p, min(q, r)));

        return *D.get(&(m, n)).unwrap();

I’ve actually also solved the same thing in JAVA, which I have a much better command on…

public class EditDistance {
    public static int minDistance(String word1, String word2) {
        int m = word1.length();
        int n = word2.length();
        int()() D = new int(m+1)(n+1);

        for(int i=0; i<=m; ++i) D(i)(0) = i;
        for(int j=0; j<=n; ++j) D(0)(j) = j;

        for(int i=1; i<=m; ++i) {
            for(int j=1; j<=n; ++j) {
                if(word1.charAt(i-1) == word2.charAt(j-1)) D(i)(j) = D(i-1)(j-1);
                else D(i)(j) = 1 + Math.min(D(i-1)(j-1), Math.min(D(i-1)(j), D(i)(j-1)));
        return D(m)(n);

    public static void main(String() args) {
        System.out.println(minDistance("intention", "execution"));

Something about my Rust solution seems off to me. Not really sure but here are some of my irritations:

  1. I couldn’t find a better way to iterate a string and refer to it by its index easily other than having to convert it to a vector.

  2. I think I’m unwrap()-ing too much. Although I’m not aiming for code golf here, it still feels subconsciously that I could have avoided so many unwrap()s.

  3. I feel a bit bad because I kind of word to word translated my solution from Java… perhaps there’s a more Rust-idiomatic way of doing whatever I did?

Looking forward to your advise and help 🙂 Thanks in advance!