Code Monkey home page Code Monkey logo

scalacaster's Introduction

Why Scalacaster?

Join the chat at https://gitter.im/vkostyukov/scalacaster

Since Fender Stratocaster is a classic guitar, Scalacaster is about classic algorithms and data structures in Scala. Scalacaster includes loads of widely used implementation techniques and approaches, which have been developed by best programmers and enthusiasts of functional programming. Studying purely functional data structures is always fun and challenge for researchers, since data structures in a functional setting are much elegant and smarter than in an imperative setting.

How to use Scalacaster?

Scalacaster is neither a library nor framework. Moreover, Scalacaster`s code is not supposed to be executed at all. Scalacaster's code is not for Scala compiler but for human beings, for enthusiasts and researchers of the Scala programming language and its application in the area of implementation of the purely functional data structures. So, the best way to use Scalacaster is to read through its source code and comments.

What is inside?

Primitive routines
Simple Collections
Heaps
Trees
Graphs
Sorting Algorithms
Searching Algorithms

How to contribute?

  • Give it a star
  • Drop the feedback to the author @vkostyukov
  • Send a PR with fixes of typos/bugs/etc

What to read next?


Creative Commons License
This work is licensed under a Creative Commons Attribution 4.0 International License.

scalacaster's People

Contributors

abbi-gaurav avatar archie avatar benfradet avatar bitdeli-chef avatar bradcater avatar drewnoff avatar gitter-badger avatar ioreskovic avatar kachayev avatar kanterov avatar lrodero avatar mukund109 avatar satybald avatar vkostyukov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

scalacaster's Issues

matchRabinKarp, strange bug

matchRabinKarp( "\"123456\"", "123456") = -1
matchRabinKarp( "\"aaaaaa\"", "aaaaaa") = -1
matchRabinKarp( "??????\"beebop\"??????????????", "beebop") = -1

(6 until 60 by 6).map( n => matchRabinKarp("\"" + "c"*n + "\"", "c"*n )) = Vector( -1, -1, ... -1 )

Can increase use of tail recursion

There are a couple of functions that could easily be rewritten to make them tail recursive, which of course would make stack overflows impossible. For example, isPalindrome().

Classic-modern interpretations

Checking the isPalindrome... The classic algorithms are expressed as an "character-by-character algorithm", and is fine. Is oriented to a low-level view of algoritms and data-structures.

Another classic manner to view pattern-recognition algoritms in strings, is to express it by regular expression

  • isPalindrome() by "anchored recursive" regular expression ^((.)(?:(?1)|.?)\2)$

  • containsPalindrone() by recursive regular expression, supposing only letters (\w)(?:(?R)|\w?)\1

It is a good view from a first abstraction layer of strings... And is also classic.


The best layer for an algorithm perhaps is where the simplest and faster algorithm is expressed. It is also "classic". In the isPalindrome() an intermediary layer of abstraction, the ideal to express the same algorithm, is by use the string.reverse() method to compare strings.

SelectionSearch may fail when asking for nth number outside index range.

Consider following example:
Asking for selectionSearch(List(5, 8, 4, 7, 3, 0, 1, 9, 6, 2), 10) should yield None, since there is no 10th element in 0-indexed list with 10 elements.

However, this call will return Some(9), which is a symptom of the bug in here:

def search(t: (List[A], A, List[A]), m: Int): Option[A] = t match {
    case (Nil, p, Nil) => Some(p)
    case (l, p, g) => select(l, p, g, l.length, m)
}

If you consider the call on singleton list with m outside range it will look like this:
search((Nil, 9, Nil), 1)

Which will return Some(9), even though m is outside the range.

The fix is to use the first pattern with guard if m == 0 and have the one without it that returns None:

def search(t: (List[A], A, List[A]), m: Int): Option[A] = t match {
    case (Nil, p, Nil) if m == 0 => Some(p)
    case (Nil, p, Nil) => None
    case (l, p, g) => select(l, p, g, l.length, m)
}

largestCommonSubstring

Strings.longestCommonSubstring gives incorrect results if the first string is shorter than the second one, because it only scans until the length of the first string:

object Strings {
...

def longestCommonSubstring(a: String, b: String) : String = {
def loop(m: Map[(Int, Int), Int], bestIndices: List[Int], i: Int, j: Int) : String = {
if (i > a.length) {
b.substring(bestIndices(1) - m((bestIndices(0),bestIndices(1))), bestIndices(1))
...

Doesn't work:
scala> val lcs = Strings.longestCommonSubstring("the price Is right", "right on!")
lcs: String = ri

Works correctly:
scala> val lcs = Strings.longestCommonSubstring("right on!", "the price is right")
lcs: String = right

why not just diagram a true tree?

  /**
   * Converts this tree into the string representation.
   *
   * Time - O(n)
   * Space - O(log n)
   */
  override def toString: String = 
    if (isEmpty) "."
    else "{" + left + value + right + "}"

hi, I learned a lot from your code. thanks.
IN the Tree data structure, the toString method is not visual friendly. why not just paint a tree use diagram.

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.