Skip to content

Getting started

Iván Corrales Solera edited this page Dec 2, 2018 · 3 revisions

Getting started

Welcome! I am excited that you want to know about Koazee. The Koazee Getting started will guide you to get your project ready.

What's Koazee?

Koazee is a handy Golang library focused on helping developers and make their life easier by taking the hassle out of working with arrays. It takes an array and creates an stream. The stream can be easily manipulated by making use of the provided operations by Koazee.. Koazee is inspired in two of the most powerful, and well-known, techniques in Software development.

Lazy evaluation: Your stream operations will only be performed when required.

  • Don't evaluate unnecessary code
  • Do the things when they need to be done!

Functional programming: It helps us to write better code.

  • It allows you to write more compressed and predictable code.
  • It’s easier to test.

How does Koazee work?

Koazee takes advantage of reflection and simplify the way to deal with arrays. It provides us with a great and growing set of powerful operations over arrays.

Installing

Add Koazee to your project

Go modules

module github.com/me/project
require ( 
  github.com/wesovilabs/koazee vX.Y.Z
)

Glide

glide get github.com/wesovilabs/koazee

Go dep

go get github.com/wesovilabs/koazee

Usage

Once Koazee is added to the project you just need to import it

package main

import (
  "fmt"
  "github.com/wesovilabs/koazee/stream"
  "koazee-samples/database"
  "strings"
)

var quotesStream = stream.New(database.GetQuotes())

func printQuotesOrderedByAuthor() stream.Stream {
  return quotesStream.
    Sort(func(quoteLeft, quoteRight *database.Quote) int {
      return strings.Compare(quoteLeft.Author, quoteRight.Author)
    }).
    ForEach(func(quote *database.Quote) {
      fmt.Printf(" * %s said \"%s\"\n", quote.Author, quote.Text)
    })
}

func numberOfAnonymousQuotes() int {
  count, _ := quotesStream.Filter(func(quote *database.Quote) bool {
    return quote.Author == "Anonymous"
  }).Count()
  return count
}

func listOfAuthors() stream.Stream {
  return quotesStream.
    Map(func(quote *database.Quote) string {
      return quote.Author
    }).
      RemoveDuplicates().
      Sort(strings.Compare)
}

func printNameOfAuthors() stream.Stream {
  return listOfAuthors().
  	ForEach(func(author string) {
      fmt.Printf(" * %s\n", author)
    })
}

func quotesByAuthorOrderedByQuoteLen(author string) stream.Stream {
  return quotesStream.
  	Filter(func(quote *database.Quote) bool {
      return quote.Author == author
    }).
    Map(func(quote *database.Quote) string {
      return quote.Text
    }).
    Sort(func(quote1, quote2 string) int {
      if len(quote1) > len(quote2) {
        return 1
      } else if len(quote1) < len(quote2) {
        return -1
      }
      return 0
    }).
    ForEach(func(quote string) {
      fmt.Println(quote)
    })
}



func main() {
  count, _ := quotesStream.Count()
  fmt.Printf("\n - Total quotes: %d\n", count)
  fmt.Printf("\n - Total anonymous quotes: %d\n", numberOfAnonymousQuotes())
  count, _ = listOfAuthors().Count()
  fmt.Printf("\n - Total authors: %d\n", count)
  fmt.Println("\nPrinting quotes ordered by author name")
  fmt.Println("--------------------------------------")
  printQuotesOrderedByAuthor().Do()
  fmt.Println("\nPrinting list of authors sorted by name")
  fmt.Println("--------------------------------------")
  printNameOfAuthors().Do()
  fmt.Println("\nPrinting list of quotes sorted bylen of quote and said by Albert Einstein")
  quotesByAuthorOrderedByQuoteLen("Albert Einstein").Do()
  fmt.Println("\nPrinting list of quotes sorted bylen of quote and said by anonymous")
  quotesByAuthorOrderedByQuoteLen("Anonymous").Do()
}