MCSN-project2014 / APproject

Project for the MCSN Advanced Programming Module A.Y. 2014/2015
http://mcsn-project2014.github.io/APproject/
MIT License
4 stars 0 forks source link

new ASYNC structure #28

Closed dido18 closed 9 years ago

dido18 commented 9 years ago

We have these possibility: luca has interpreted the first choice.

 let Aminus n =
     async{
        return n-1
    }

let minus n =
    n-1

let  taskA = Async.StartAsTask(  Aminus 7    )
let  taskB = Async.StartAsTask( async{ return 7 - 1  }  )
let  taskC = Async.StartAsTask( async{ return minus 7  }  )

taskA.Wait()
taskB.Wait()
taskC.Wait()

printf " is task a %d \n" taskA.Result
printf " is task b %d \n" taskB.Result
printf " is task c %d \n" taskC.Result

My choice is that an Async node is translated always as: Async.StartAsTask( async{ return .... } ) Otherwise we must have two asyncNodes or a field in the async node that indicates that it hasn't a return value

I think that we should have a new Types 'TaskAsync' in order to discover when we must introdue a waiting condition in the f# code.

Mene90 commented 9 years ago
fun add(x int, y int) int{
    return x+y;
}

fun minus(x int, y int) int{
    return x-y;
} 

fun main(){
    var result int = 0;
    var value int;
    var op int = 0;
    while op != 9 {
        value = readln();
        if op == 1 {
            result = async{return add(result,value)};
        }
        if op==2 {
            result = async{return minus(result,value)};
        }
        op = readln();
    }
    println(result);
}

This funW@p program have to be traslate in F# in the following way:

open System
open System.IO
open System.Threading.Tasks

let rec add x  y  = 
    x + y

let rec minus x  y  = 
    x - y

[<EntryPoint>]
let main argv = 
    let _task = ref( Map.empty )  //Map to manage the open task

    let result = ref 0
    let  value = ref 0
    let op = ref 1

    while !op <> 9 do
        value := Convert.ToInt32(Console.ReadLine())

        if !op = 1 then
            //before use the variable of a async//
            if (!_task).ContainsKey(result) then 
                let task : Task<int> = (!_task).[result] 
                result := task.Result
             //--------///
            _task := (!_task).Add(result, Async.StartAsTask( async{ return add  (!result)  (!value)} ))
        if !op = 2 then
            //before use the variable of a async//
            if (!_task).ContainsKey(result) then 
                let task : Task<int> = (!_task).[result] 
                result := task.Result
            //-----//
            _task := (!_task).Add(result, Async.StartAsTask( async{ return minus  (!result)  (!value) } ))

        op := Convert.ToInt32(Console.ReadLine())

    //before use the variable of a async//
    if (!_task).ContainsKey(result) then 
        let task : Task<int> = (!_task).[result] 
        result := task.Result
    //-----//
    Console.WriteLine(!result)
    Console.ReadLine() |> ignore
    0

@teto1992