Git Product home page Git Product logo

qod's Introduction

qod

See the Documentation

Package qod should NOT be used in a serious software engineering environment. qod stands for Quick and Dirty bahaha I just realized I got the acronym wrong. It's fine. It's on brand. Quick AND Dirty.

The context is I noticed that Go is my favorite language, but when a task gets too complicated for a shell pipeline or awk or something, I turn to Python. Why not Go?

In Python, I'd frequently write something like:

for line in sys.stdin:
  vals = map(int, line.split())

Here that is in Go:

package main

import (
  "bufio"
  "fmt"
  "os"
  "strconv"
  "strings"
)

func main() {
  scanner := bufio.NewScanner(os.Stdin)
  for scanner.Scan() {
    var vals []int64
    for _, str := range strings.Fields(scanner.Text()) {
      val, err := strconv.ParseInt(str, 10, 64)
      if err != nil {
        panic(err)
      }
      vals = append(vals, val)
    }
  }
  if err := scanner.Err(); err != nil {
    panic(err)
  }
}

Ugh! Considering I don't care about this throwaway shell pipeline replacement, I'm clearly fine with it blowing up if something's wrong, and wow this was too much.

qod allows me to write the same type of thing in Go. Here is a reimplementation of the Python code above using qod:

package main

import (
  "os"
  "strings"

  "github.com/jtolds/qod"
)

func main() {
  for line := range qod.Lines(os.Stdin) {
    vals := qod.Int64Slice(strings.Fields(line))
  }
}

Better! I'm more likely to use Go now for little scripts!

Reminder: don't use this for anything real. Most of the stuff in here panics at the sight of any errors. That's obviously Bad and Wrong and you should actually handle your errors. Set up your build system's linter to reject anything that imports github.com/jtolds/qod please. If you have a build system for what you're doing at all this isn't for you. If you have some one-off tab-delimited data you need to process real quick like I seem to ALL THE TIME then okay.

License

Copyright (C) 2017 JT Olds. See LICENSE for copying information.

qod's People

Contributors

jtolio avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

qod's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.