Skip to content

Commit 6d6ad41

Browse files
committed
Fix last lectures
1 parent e62a220 commit 6d6ad41

File tree

7 files changed

+48
-68
lines changed

7 files changed

+48
-68
lines changed

docs/06-Pure-Functional-Programming-with-Haskell.slide

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,11 @@ Here, the function *add* takes two arguments *a* and *b* and returns *a* + *b*
175175
add a b = a + b
176176
main = print( (add 1) 2)
177177

178+
* The return function
179+
180+
TODO
181+
182+
178183
* Haskell: Type signatures
179184

180185
add :: Int -> Int -> Int

docs/07-Concurrent-Programming.slide

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ https://www.qaware.de
1717
* Why Concurrent Programming?
1818
- Computer clock rates do not get higher anymore (since 2004!)
1919
- But Moores Law is still valid (Multicore!)
20-
.image img/06-moores-law.png 450 800
20+
21+
.image ./img/06-moores-law.webp 450 _
2122

2223
: https://github.com/qaware/cloudcomputing
2324

@@ -218,10 +219,14 @@ What will be the result?
218219

219220
- Atomics either use a atomic CPU feature or perform a low level blocking operation via a mutex
220221

221-
* Waitgroups
222+
* Waitgroups (using global variableI)
222223

223224
.play ../src/concurrent/waitgroup/parallel_for_loop.go /func/,
224225

226+
* Waitgroups (using functional programming)
227+
228+
.play ../src/concurrent/waitgroup2/parallel_for_loop.go /func/,
229+
225230
* Excercise
226231

227232
This is the previous code for atomic counting.

docs/Details-semester-work.slide

Lines changed: 0 additions & 59 deletions
This file was deleted.

docs/img/06-moores-law.webp

8.83 KB
Binary file not shown.

src/concurrent/waitgroup/parallel_for_loop.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@ import (
66
"sync"
77
)
88

9+
var wg sync.WaitGroup
10+
911
func ParallelFor(n int, f func(int)) {
10-
var wg sync.WaitGroup
1112
wg.Add(n)
1213
for i := 0; i < n; i++ {
13-
go func(i int) {
14-
defer wg.Done()
15-
f(i)
16-
}(i)
14+
go f(i)
1715
}
1816
wg.Wait()
1917
}
@@ -22,6 +20,7 @@ func ProbablyPrime(value int) {
2220
if big.NewInt(int64(value)).ProbablyPrime(0) == true {
2321
fmt.Printf("%d is probably prime\n", value)
2422
}
23+
wg.Done()
2524
}
2625

2726
func main() {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"math/big"
6+
"sync"
7+
)
8+
9+
func ParallelFor(n int, f func(int)) {
10+
var wg sync.WaitGroup
11+
wg.Add(n)
12+
for i := 0; i < n; i++ {
13+
go func(i int) {
14+
defer wg.Done() // is always called before the goroutine exits
15+
f(i)
16+
}(i)
17+
}
18+
wg.Wait()
19+
}
20+
21+
func ProbablyPrime(value int) {
22+
if big.NewInt(int64(value)).ProbablyPrime(0) == true {
23+
fmt.Printf("%d is probably prime\n", value)
24+
}
25+
}
26+
27+
func main() {
28+
fmt.Println("Start Program")
29+
ParallelFor(1000, ProbablyPrime)
30+
fmt.Println("Stop Program")
31+
}

src/haskell/guessing_game.hs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,12 @@ status guess secret | guess == secret = do
2020
return False
2121

2222
play :: Int -> IO ()
23-
play (-1) = return ()
2423
play secret = do
2524
input <- getLine
2625
let guess = read input :: Int
2726
isWin <- Main.status guess secret
2827
if isWin then
29-
play (-1)
28+
return ()
3029
else do
3130
play secret
3231

0 commit comments

Comments
 (0)