Skip to content

Instantly share code, notes, and snippets.

View abdolence's full-sized avatar

Abdulla Abdurakhmanov abdolence

View GitHub Profile

Keybase proof

I hereby claim:

  • I am abdolence on github.
  • I am abdolence (https://keybase.io/abdolence) on keybase.
  • I have a public key whose fingerprint is BE07 447F 3877 E909 9543 FB29 8FDF 9099 4F0D 7D1F

To claim this, I am signing this object:

@abdolence
abdolence / fizzbuss1.scala
Last active March 19, 2020 21:06
medium-fizzbuss-straightforward-algo
def basicFizzbuzzRules( x: Int ): String = {
( x % 3, x % 5 ) match {
case ( 0, 0 ) => "FizzBuzz"
case ( 0, _ ) => "Fizz"
case ( _, 0 ) => "Buzz"
case _ => x.toString
}
}
def fizzbuzz( n: Int )( rules: Int => String ): LazyList[String] = {
case class FizzBuzzRuleList[A]( rules: FizzBuzzRule[A]* ) {
def map( x: A ): String =
rules
.find( _.pred( x ) )
.map( _.value.apply() )
.getOrElse( x.toString )
}
object FizzBuzzRuleList {
import scala.collection.parallel.CollectionConverters._
import scala.collection.parallel.ParSeq
def parallelFizzbuzz( n: Int )( rules: Int => String ): ParSeq[( Int, String )] = {
(1 to n).par
.map(x => ( x, rules( x ) ) )
}
parallelFizzbuzz( N )( basicFizzbuzzRules ).foreach( println )
import akka.actor.ActorSystem
import akka.stream._
import akka.stream.scaladsl._
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext.Implicits.global
def reactiveFizzbuzz( n: Int )( rules: Int => String ): Source[( Int, String ), _] = {
Source(
1 to n
package fizzbuzz
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
import cloudflow.akkastream._
import cloudflow.akkastream.util.scaladsl._
import cloudflow.streamlets._
import cloudflow.streamlets.avro._
package fizzbuzz
import cloudflow.akkastream._
import cloudflow.akkastream.scaladsl._
import cloudflow.streamlets.{ RoundRobinPartitioner, StreamletShape }
import cloudflow.streamlets.avro._
class FizzBuzzProcessor extends AkkaServerStreamlet {
def basicFizzbuzzRules( x: Int ): String = {
package fizzbuzz
import cloudflow.akkastream._
import cloudflow.akkastream.scaladsl._
import cloudflow.streamlets._
import cloudflow.streamlets.avro._
class FizzBuzzPrinter extends AkkaStreamlet {
val inlet = AvroInlet[FizzBuzzResult]( "in" )
val shape = StreamletShape.withInlets( inlet )
// FizzBuzzNumber.avsc
{
"namespace": "fizzbuzz",
"type": "record",
"name": "FizzBuzzNumber",
"fields":[
{
"name": "value",
"type": {
"type": "int"
val conf = new SparkConf()
//.setMaster("local") for local development
.setAppName("FizzBuzz App")
val spark = SparkSession
.builder()
.config(conf)
.getOrCreate()
import spark.implicits._