Skip to content

Commit

Permalink
Setup Repo (#1)
Browse files Browse the repository at this point in the history
* Setup Repo

* Fix workflow
  • Loading branch information
theMagicalKarp committed Mar 21, 2024
1 parent a1e3254 commit 3ffacbf
Show file tree
Hide file tree
Showing 134 changed files with 7,938 additions and 3 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: 🧪 Test/Lint

on:
push:
branches:
- main
pull_request:

permissions:
contents: read

jobs:
test-lint:
name: Test/Lint
runs-on: ubuntu-latest
strategy:
matrix:
go-version: [ '1.21', '1.22' ]

steps:
- uses: actions/checkout@v4

- name: Setup Go ${{ matrix.go-version }}
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}

- name: Tests
run: go test ./...

- name: Linter
uses: golangci/golangci-lint-action@v4
with:
version: v1.56.2
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@

# Dependency directories (remove the comment below to include it)
# vendor/

.DS_Store
112 changes: 112 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
run:
skip-files:
- ".*_test.go"

linters:
enable:
- asasalint
- asciicheck
- bidichk
- bodyclose
#! - containedctx
- contextcheck
#! - copyloopvar
- cyclop
- decorder
#! - depguard
- dogsled
- dupl
- dupword
- durationcheck
- errcheck
- errchkjson
- errname
- errorlint
- execinquery
- exhaustive
#! - exhaustruct
- exportloopref
#! - forbidigo
- forcetypeassert
- funlen
- gci
- ginkgolinter
- gocheckcompilerdirectives
- gochecknoglobals
- gochecknoinits
- gochecksumtype
- gocognit
- goconst
- gocritic
- gocyclo
- godot
- godox
- goerr113
- gofmt
- gofumpt
- goheader
- goimports
- gomnd
- gomoddirectives
- gomodguard
- goprintffuncname
- gosec
- gosimple
- gosmopolitan
- govet
- grouper
- importas
- inamedparam
- ineffassign
- interfacebloat
#! - intrange
#! - ireturn
- lll
- loggercheck
- maintidx
- makezero
- mirror
- misspell
- musttag
- nakedret
- nestif
- nilerr
- nilnil
- nlreturn
- noctx
- nolintlint
- nonamedreturns
- nosprintfhostport
- paralleltest
- perfsprint
- prealloc
- predeclared
- promlinter
- protogetter
- reassign
- revive
- rowserrcheck
- sloglint
- spancheck
- sqlclosecheck
- staticcheck
- stylecheck
- tagalign
- tagliatelle
- tenv
- testableexamples
- testifylint
- testpackage
- thelper
- tparallel
- typecheck
- unconvert
- unparam
- unused
- usestdlibvars
- varnamelen
- wastedassign
- whitespace
- wrapcheck
- wsl
- zerologlint
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2022 Robert Sheehy
Copyright (c) 2024 Robert Sheehy

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
78 changes: 76 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,76 @@
# iter
Go Library for Iterators
# Iter

Iter is a _simple_ and _ergonomic_ package for implementing and utilizing
iterators in Golang.

## Features

- Easy to implement Iterable interface
- Standardized toolset for utilizing iterables

## Limitions

While this package aims to provide a fluid way of writing code, it also strives
to be as efficient as possible. However, due to the inherent nature and
idiosyncrasies of Go, it will always be more efficient to write code that
utilizes the language's primitives.

This package does not aim to be the fastest way to run Go.

## Getting Started

```Go
package main

import (
"github.com/theMagicalKarp/iter/pkg/iter"
"github.com/theMagicalKarp/iter/pkg/itertools"
)

func main() {
// create iterator from static values
numbers := iter.New(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)

// filter to collect even numbers
evenNumbers := itertools.Filter(numbers, func(n int) bool {
return n%2 == 0
})

// square numbers and convert to strings
squaredNumbers := itertools.Map(evenNumbers, func(n int) int {
return n*n
})

itertools.Print(squaredNumbers)
// 0, 4, 16, 36, 64
}
```

## How

Everything in the iter packages utilizes a single interface for reading data.

```Go
// Iterable represents a generic iterable collection.
type Iterable[T any] interface {
// Next returns the next element in the collection and a boolean value indicating if the value was found.
Next() (T, bool)
}
```

## Why

Universal iterable interfaces are foundational to powerful problem solving. When
using iterables as the standard for functional communication, you can create and
utilize standard sets of libraries and structs seamlessly. This results in
clever and easy-to-understand solutions.

Since many of the itertools functions accept and return iterables, you can plug
and play various functions to achieve a desired result.

## Test

```Shell
go test ./...
golangci-lint run
```
7 changes: 7 additions & 0 deletions examples/advent_2023_day_1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## What

Solution to part 2 of https://adventofcode.com/2023/day/1.

```
go run main.go
```
74 changes: 74 additions & 0 deletions examples/advent_2023_day_1/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package main

import (
"fmt"
"os"
"strings"
"unicode"

"github.com/theMagicalKarp/iter/pkg/itertools"
"github.com/theMagicalKarp/iter/pkg/predicates"
)

var WORD_TO_DIGIT = map[string]int{
"one": 1,
"two": 2,
"three": 3,
"four": 4,
"five": 5,
"six": 6,
"seven": 7,
"eight": 8,
"nine": 9,
}

func ToDigit(s string) int {
if unicode.IsDigit(rune(s[0])) {
return int(s[0] - '0')
}

for k, v := range WORD_TO_DIGIT {
if strings.HasPrefix(s, k) {
return v
}
}

return -1
}

func ChopString(s string) func(int) string {
return func(i int) string {
return s[i:]
}
}

func ProcessLine(line string) int {
irange := itertools.Range(0, len(line))
subStrings := itertools.Map(irange, ChopString(line))
allDigits := itertools.Map(subStrings, ToDigit)
positiveDigits := itertools.Filter(allDigits, predicates.Positive)

first, more := positiveDigits.Next()
if !more {
return 0
}

last, found := itertools.Last(positiveDigits)
if !found {
last = first
}

return first*10 + last
}

func main() {
f, err := os.Open("examples/example1/input.txt")
if err != nil {
panic(err)
}
defer f.Close()

lines := itertools.Lines(f)
readings := itertools.Map(lines, ProcessLine)
fmt.Println(itertools.Sum(readings))
}
7 changes: 7 additions & 0 deletions examples/advent_2023_day_2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## What

Solution to part 2 of https://adventofcode.com/2023/day/2.

```
go run main.go
```
Loading

0 comments on commit 3ffacbf

Please sign in to comment.