How convert array of observable to observable of array?
func f1: string -> Observable<T>
o: Observable<string[]>
o.map( x => x.map( y=> f1(y)))
.mergeAll()
return is Observable<T>[].
How can i get it ?
Correct solution :
o.mergeMap( x=> Observable.from(
x.map( y => f1(y) ).mergeAll().toArray()
)
)
or if not exist mergeMap():
o.map( x=> Observable.from(
x.map( y => f1(y) ).mergeAll().toArray()
)
).mergeAll()
Related
I am trying to obtain a behavior such that if current observable is changed in value, the respective observables linked should be invoked respectively.
The following code works as intended, but I just need some conformation regarding this behavior, I am trying to implement this on production site.
I know switchMap unsubscribes from current subscription and resubscribes to new one that's returned.
But, Does it also call the new observable subscribed to kick in and run the code it has or is this run by the pipe operator or am I missing any crucial concept ?
Can some one kindly, clarify this.
Thank you.
Here is the stackblitz code link ::: https://stackblitz.com/edit/rxjs-ktnf9x :: and Overview
let a = new BehaviorSubject(null);
let b = new BehaviorSubject(null);
let c = new BehaviorSubject(null);
let d = new BehaviorSubject(null);
let a$ = a.asObservable();
let b$ = b.asObservable();
let c$ = c.asObservable();
let d$ = d.asObservable();
d$
.pipe(
switchMap(
data => {
console.log("from d :: " + data);
return c$;
}
)
)
.pipe(
switchMap(
data => {
console.log("from c :: " + data);
return b$;
}
)
)
.pipe(
switchMap(
data => {
console.log("from b :: " + data);
return a$;
}
)
)
.subscribe(
data => {
console.log("from a :: " + data);
console.log(""); // for next line.
}
)
b.next("calls");
a.next("allSubs");
c.next("it");
d.next("does");
d.next('yes');
finally --> Outputs ::: yes it calls allSubs
To make it simpler to explain. SwitchMap unsubscribe c$ when d$ emits. if d emit once and c keeps emitting you will get a sequence of d-c-c-c
if d emits again in the middle you will get d-c-c-c-d-c-c-c
d$
.pipe(
switchMap(
data => {
console.log("from d :: " + data);
return c$;
}
)
)
I tried to make RPN in scala but i got Exception in console
Exception in thread "main" scala.MatchError: 1 (of class
java.lang.Character)
and i stop here because Intelij don't show where is problem . I think that i 'am wrong making
if (!expr.isEmpty) {
expr.head match
but i spend some hours thinking how to exchange this and i din't invent anything
i make this program in Java but in functional programming i don't know how to correctly use this match with tail Recursive
object RPN extends App {
print(evaluate("123++"))
def evaluate( expr : String) {
val stack = Stack[Double]()
var a,b :Int=0
#tailrec
def helper ( asset : String){
if (!expr.isEmpty) {
expr.head match {
case it if 0 until 9 contains it => stack.push(expr.head); helper(asset.tail)
case '+' => b = stack.pop.toString().toInt; a = stack.pop.toString().toInt; stack.push(a + b)
case '-' => b = stack.pop.toString().toInt; a = stack.pop.toString().toInt; stack.push(a - b)
case '*' => b = stack.pop.toString().toInt; a = stack.pop.toString().toInt; stack.push(a * b)
case '/' => b = stack.pop.toString().toInt; a = stack.pop.toString().toInt; stack.push(a / b)
}
}
else return stack.pop
}
helper(expr)
}
}
Your code is referencing expr inside of helper(), where you probably want to reference asset instead.
Also, you don't need the return or the vars.
def evaluate(expr :String) :Double = {
val stack = collection.mutable.Stack[Double]()
#annotation.tailrec
def helper(asset :String) :Double =
if (asset.isEmpty) stack.pop
else {
asset.head match {
case c if c.isDigit =>
stack.push(c.asDigit)
case '+' => stack.push(stack.pop + stack.pop)
case '-' => stack.push(-stack.pop + stack.pop)
case '*' => stack.push(stack.pop * stack.pop)
case '/' => stack.push(1/stack.pop * stack.pop)
case c => throw new Error(s"Bad Char: $c")
}
helper(asset.tail)
}
helper(expr)
}
testing:
evaluate("123++") //res0: Double = 6.0
evaluate("954--") //res1: Double = 8.0
evaluate("423*+") //res2: Double = 10.0
evaluate("28/") //res3: Double = 0.25
evaluate("73/") //res4: Double = 2.333333333333333
I have an array of subarrays in the following format
Array
(
[0] => Array
(
[unit_id] => 6504
[assignment_name] => Grade assignment
[assignment_description] =>
[assignment_total_score] => 10
[unit_type_name] => Homework
[is_graded] => 1
[standard_id] => 1219
[scoring_type] => score
[attempt_score] => 8
[unit_duedate] => 2016-02-10 09:00:00
[standard] => Array
(
[0] => stdClass Object
(
[unit_id] => 6504
[is_formal] => 1
[assignment_name] => Grade assignment
[assignment_description] =>
[standard_id] => 1220
[standard_name] => 9-10.RL.3
[standard_description] => Analyze how complex characters (e.g., those with multiple or conflicting motivations) develop over the course of a
)
)
)
[1] => Array
(
[unit_id] => 8584
[assignment_name] => Sine and Cosecant Graphs
[assignment_description] => Define the sine and cosecant graphs using a unit circle
[assignment_total_score] => 15
[unit_type_name] => Paper
[scoring_type] => score
[attempt_score] => 0
[unit_duedate] => 2016-04-29 09:00:00
[standard] => Array
(
[0] => stdClass Object
(
[unit_id] => 8584
[is_formal] => 1
[assignment_name] => Sine and Cosecant Graphs
[assignment_description] => Define the sine and cosecant graphs using a unit circle
[assignment_total_score] => 15
[standard_id] => 82790
[standard_name] => 9-10.RL.7
)
)
[2] => Array
(
[unit_id] => 11611
[assignment_name] => Adding 5 + 3 + 6
[assignment_description] =>
[assignment_total_score] => 10
[unit_type_name] => Homework
[standard_id] => 82772
[scoring_type] => score
[attempt_score] => 0
[unit_duedate] => 2016-08-23 19:00:00
[standard] => Array
(
[0] => stdClass Object
(
[unit_id] => 11611
[is_formal] => 1
[assignment_name] => Adding 5 + 3 + 6
[assignment_description] =>
[assignment_total_score] => 10
[standard_id] => 82772
[standard_name] => 9-10.RL.1
)
)
)
)
And I would like to group it into a new slice based on the unit_type_name field in each subarray.
How can I group the slice by unit_type_name? Is there any native Go functions are available to do this?
if I for loop the above then I will get a duplicate one, how can I avoid that?
I do not think Go has a built-in functionality to help you do that (I may be wrong). My Assumption is that the PHP array will be converted to a JSON object. I managed to get the code below to help you sort your array (In JSON format) a based on the unit_type_name
I created two structs which have JSON values similar to what the array keys would be
//StandardType ...
type StandardType struct {
UnitID int `json:"unit_id"`
IsFormal int `json:"is_formal"`
AssignmentName string `json:"assignment_name"`
AssignmentDescription string `json:"assignment_description"`
StandardID int `json:"standard_id"`
StandardName string `json:"standard_name"`
StandardDescription string `json:"standard_description"`
}
//AutoGenerated ...
type AutoGenerated struct {
UnitID int `json:"unit_id"`
AssignmentName string `json:"assignment_name"`
AssignmentDescription string `json:"assignment_description"`
AssignmentTotalScore int `json:"assignment_total_score"`
UnitTypeName string `json:"unit_type_name"`
IsGraded int `json:"is_graded"`
StandardID int `json:"standard_id"`
ScoringType string `json:"scoring_type"`
AttemptScore int `json:"attempt_score"`
UnitDuedate string `json:"unit_duedate"`
Standard []StandardType `json:"standard"`
}
var jsonData = ``
func main() {
m := []AutoGenerated{}
err := json.Unmarshal([]byte(jsonData), &m)
if err != nil {
panic(err)
}
I created a map to hold the unit_type_name keys
sliceKeys := make(map[string]string)
I created also map to hold the arrays that have similar unit_type_name keys in an AutoGenerated array
groupedSlices := make(map[string][]AutoGenerated)
Then I loop through the decoded JSON string searching for the unit_type_name
for i := range m {
If a unit_type_name already exists in the key slice I add the array item to the group slice
if _, ok := sliceKeys[m[i].UnitTypeName]; ok {
autogenerated := groupedSlices[m[i].UnitTypeName]
autogenerated = append(autogenerated, m[i])
groupedSlices[m[i].UnitTypeName] = autogenerated
} else {
Else I create a new array key and add the item to it
sliceKeys[m[i].UnitTypeName] = m[i].UnitTypeName
autogenerated := []AutoGenerated{}
autogenerated = append(autogenerated, m[i])
groupedSlices[m[i].UnitTypeName] = autogenerated
}
}
fmt.Println(sliceKeys)
fmt.Println(groupedSlices)
}
input:
[{"unit_id": 6504,"assignment_name": "Grade assignment","assignment_description": "","assignment_total_score": 10,"unit_type_name": "Homework","is_graded": 1,"standard_id": 1219,
"scoring_type": "score","attempt_score": 8,"unit_duedate": "2016-02-10 09:00:00",
"standard": [{"unit_id": 6504,"is_formal": 1,"assignment_name": "Grade assignment","assignment_description": "",
"standard_id": 1220,"standard_name": "9-10.RL.3","standard_description": "Analyze how complex characters (e.g., those with multiple or conflicting motivations) develop over the course of a "
}]},{"unit_id": 6504,"assignment_name": "Grade assignment","assignment_description": "","assignment_total_score": 10,
"unit_type_name": "Paper","is_graded": 1,"standard_id": 1219,"scoring_type": "score","attempt_score": 8,"unit_duedate": "2016-02-10 09:00:00","standard": [{"unit_id": 6504,"is_formal": 1,"assignment_name": "Grade assignment","assignment_description": "","standard_id": 1220,"standard_name": "9-10.RL.3","standard_description": "Analyze how complex characters (e.g., those with multiple or conflicting motivations) develop over the course of a "}]},{
"unit_id": 6504,"assignment_name": "Grade assignment","assignment_description": "",
"assignment_total_score": 10,"unit_type_name": "Aything else","is_graded": 1,"standard_id": 1219,
"scoring_type": "score","attempt_score": 8,"unit_duedate": "2016-02-10 09:00:00","standard": [{
"unit_id": 6504,"is_formal": 1,"assignment_name": "Grade assignment","assignment_description": "","standard_id": 1220,
"standard_name": "9-10.RL.3","standard_description": "Analyze how complex characters (e.g., those with multiple or conflicting motivations) develop over the course of a "}]}]
output:
map[Homework:Homework Paper:Paper Aything else:Aything else]
map[
Homework:[
{6504 Grade assignment 10 Homework 1 1219 score 8 2016-02-10 09:00:00 [{6504 1 Grade assignment 1220 9-10.RL.3 Analyze how complex characters (e.g., those with multiple or conflicting motivations) develop over the course of a }]}
]
Paper:[
{6504 Grade assignment 10 Paper 1 1219 score 8 2016-02-10 09:00:00 [{6504 1 Grade assignment 1220 9-10.RL.3 Analyze how complex characters (e.g., those with multiple or conflicting motivations) develop over the course of a }]}
]
Aything else:[
{6504 Grade assignment 10 Aything else 1 1219 score 8 2016-02-10 09:00:00 [{6504 1 Grade assignment 1220 9-10.RL.3 Analyze how complex characters (e.g., those with multiple or conflicting motivations) develop over the course of a }]}]
]
Here is a general GroupBy function I wrote:
// Created by BaiJiFeiLong#gmail.com at 2021/8/27 10:51
package main
import (
"fmt"
"reflect"
"strconv"
)
func GroupBy(arr interface{}, groupFunc interface{}) interface{} {
groupMap := reflect.MakeMap(reflect.MapOf(reflect.TypeOf(groupFunc).Out(0), reflect.TypeOf(arr)))
for i := 0; i < reflect.ValueOf(arr).Len(); i++ {
groupPivot := reflect.ValueOf(groupFunc).Call([]reflect.Value{reflect.ValueOf(arr).Index(i)})[0]
if !groupMap.MapIndex(groupPivot).IsValid() {
groupMap.SetMapIndex(groupPivot, reflect.MakeSlice(reflect.SliceOf(reflect.TypeOf(arr).Elem()), 0, 0))
}
groupMap.SetMapIndex(groupPivot, reflect.Append(groupMap.MapIndex(groupPivot), reflect.ValueOf(arr).Index(i)))
}
return groupMap.Interface()
}
func main() {
fmt.Println(GroupBy([]int{1, 22, 3, 44, 555}, func(value int) string {
return strconv.Itoa(len(strconv.Itoa(value)))
}))
}
The output:
map[1:[1 3] 2:[22 44] 3:[555]]
For your question, it shoube be some code like:
group := GroupBy(items, func(item Item) string {
return item.UnitTypeName
}).(map[string][]Item)
I'm new to Spark, I want to make a treatment on files in streaming.
I have files csv which arrive non-stop:
Example csv file:
world world
count world
world earth
count world
and I want to do two treatment on them :
the first treatment is for a result like this :
(world,2,2) // word is twice repeated for the first column and distinct (world,earth) for second therefore (2,2)
(count,2,1) // word is twice repeated for the first column and not distinct (world,world) for second therefore (2,1)
the second result
I want to get that result after each hour.in our example:
(world,1) // 1=2/2
(count,2) //2=2/1
this is my code :
val conf = new SparkConf()
.setAppName("File Count")
.setMaster("local[2]")
val sc = new SparkContext(conf)
val ssc = new StreamingContext(sc, Seconds(10m))
val file = ssc.textFileStream("hdfs://192.168.1.31:8020/user/sparkStreaming/input")
var result = file.map(x => (x.split(" ")(0)+";"+x.split(" ")(1), 1)).reduceByKey((x,y) => x+y)
val window = result.reduceByKeyAndWindow((a:Int,b:Int) => (a + b), Seconds(60), Seconds(20))
val result1 = window.map(x => x.toString )
val result2 = result1.map(line => line.split(";")(0)+","+line.split(",")(1))
val result3 = result2.map(line => line.substring(1, line.length-1))
val result4 = result3.map(line => (line.split(",")(0),line.split(",")(1).toInt ) )
val result5 = result4.reduceByKey((x,y) => x+y )
val result6 = result3.map(line => (line.split(",")(0), 1 ))
val result7 = result6.reduceByKey((x,y) => x+y )
val result8 = result7.join(result5) // (world,2,2)
val finalResult = result8.mapValues(x => x._1.toFloat / x._2 ) // (world,1), I want this result after every one hour
ssc.start()
ssc.awaitTermination()
Thanks in Advance!!!
While learning Scalaz 6, I'm trying to write type-safe readers returning validations. Here are my new types:
type ValidReader[S,X] = (S) => Validation[NonEmptyList[String],X]
type MapReader[X] = ValidReader[Map[String,String],X]
and I have two functions creating map-readers for ints and strings (*):
def readInt( k: String ): MapReader[Int] = ...
def readString( k: String ): MapReader[String] = ...
Given the following map:
val data = Map( "name" -> "Paul", "age" -> "8" )
I can write two readers to retrieve the name and age:
val name = readString( "name" )
val age = readInt( "age" )
println( name(data) ) //=> Success("Paul")
println( age(data) ) //=> Success(8)
Everything works fine, but now I want to compose both readers to build a Boy instance:
case class Boy( name: String, age: Int )
My best take is:
val boy = ( name |#| age ) {
(n,a) => ( n |#| a ) { Boy(_,_) }
}
println( boy(data) ) //=> Success(Boy(Paul,8))
It works as expected, but the expression is awkward with two levels of applicative builders. Is there a way, to get the following syntax to work ?
val boy = ( name |#| age ) { Boy(_,_) }
(*) Full and runnable implementation in: https://gist.github.com/1891147
Update: Here is the compiler error message that I get when trying the line above or Daniel suggestion:
[error] ***/MapReader.scala:114: type mismatch;
[error] found : scalaz.Validation[scalaz.NonEmptyList[String],String]
[error] required: String
[error] val boy = ( name |#| age ) { Boy(_,_) }
[error] ^
How about this?
val boy = (name |#| age) {
(Boy.apply _).lift[({type V[X]=ValidationNEL[String,X]})#V]
}
or using a type alias:
type VNELStr[X] = ValidationNEL[String,X]
val boy = (name |#| age) apply (Boy(_, _)).lift[VNELStr]
This is based on the following error message at the console:
scala> name |#| age apply Boy.apply
<console>:22: error: type mismatch;
found : (String, Int) => MapReader.Boy
required: (scalaz.Validation[scalaz.NonEmptyList[String],String],
scalaz.Validation[scalaz.NonEmptyList[String],Int]) => ?
So I just lifted Boy.apply to take the required type.
Note that since Reader and Validation (with a semigroup E) are both Applicative, their composition is also Applicative. Using scalaz 7 this can be expressed as:
import scalaz.Reader
import scalaz.Reader.{apply => toReader}
import scalaz.{Validation, ValidationNEL, Applicative, Kleisli, NonEmptyList}
//type IntReader[A] = Reader[Int, A] // has some ambigous implicit resolution problem
type IntReader[A] = Kleisli[scalaz.IdInstances#Id, Int, A]
type ValNEL[A] = ValidationNEL[Throwable, A]
val app = Applicative[IntReader].compose[ValNEL]
Now we can use a single |#| operation on the composed Applicative:
val f1 = toReader((x: Int) => Validation.success[NonEmptyList[Throwable], String](x.toString))
val f2 = toReader((x: Int) => Validation.success[NonEmptyList[Throwable], String]((x+1).toString))
val f3 = app.map2(f1, f2)(_ + ":" + _)
f3.run(5) should be_==(Validation.success("5:6"))