basic.erl 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. % Autogenerated with DRAKON Editor 1.26
  2. -module(basic).
  3. -export([create_user/3, echo/0, echo_t/0, future_time/1, hello/2, log_on/2, number_to_word/1, print_result/1, quad_equation/3, quicksort/2, run/0, split/2, split_core/3]).
  4. % stipan.mitkin@gmail.com
  5. % http://drakon-editor.sourceforge.net/
  6. % Copyright 2015 Stepan Mitkin
  7. % This code is PUBLIC DOMAIN.
  8. access_denied(User) ->
  9. % item 255
  10. {user, Name, _, _, _} = User,
  11. % item 256
  12. io:format("Access denied to user ~s.~n", [Name])
  13. .
  14. add_buffer(Buffer, List) ->
  15. % item 486
  16. Reversed = lists:reverse(Buffer),
  17. [Reversed | List]
  18. .
  19. attach_session(User) ->
  20. % item 248
  21. {user, Name, _, _, _} = User,
  22. % item 249
  23. io:format("User ~s has logged on.~n", [Name])
  24. .
  25. check_password(User, Password) ->
  26. % item 239
  27. {user, _, Salt, Hash, Enabled} = User,
  28. % item 236
  29. case Enabled of true ->
  30. % item 240
  31. ActualHash = make_pass_hash(Salt, Password),
  32. % item 241
  33. ActualHash == Hash
  34. ; false ->
  35. % item 242
  36. false
  37. end
  38. .
  39. create_user(Name, Password, Enabled) ->
  40. % item 222
  41. Salt = crypto:rand_bytes(8),
  42. % item 223
  43. Hash = make_pass_hash(Salt, Password),
  44. % item 221
  45. User = {
  46. user,
  47. Name,
  48. Salt,
  49. Hash,
  50. Enabled
  51. },
  52. % item 305
  53. User
  54. .
  55. echo() ->
  56. % item 541
  57. io:format(
  58. "echo: waiting...~n", []
  59. ),
  60. receive
  61. stop ->
  62. % item 542
  63. io:format("stopping~n", []),
  64. % item 531
  65. ok
  66. ;
  67. {msg, Message} ->
  68. % item 532
  69. io:format("~p~n", [Message]),
  70. % item 536
  71. echo()
  72. ;
  73. _ ->
  74. % item 539
  75. io:format("unknown~n", []),
  76. % item 536
  77. echo()
  78. end
  79. .
  80. echo_t() ->
  81. % item 561
  82. io:format(
  83. "echo_t: waiting...~n", []
  84. ),
  85. receive
  86. stop ->
  87. % item 562
  88. io:format(
  89. "stopping~n", []
  90. ),
  91. % item 553
  92. ok
  93. ;
  94. {msg, Message} ->
  95. % item 554
  96. io:format(
  97. "~p~n", [Message]
  98. ),
  99. % item 556
  100. echo_t()
  101. ;
  102. _ ->
  103. % item 559
  104. io:format(
  105. "unknown~n",
  106. []
  107. ),
  108. % item 556
  109. echo_t()
  110. after 10000 ->
  111. % item 565
  112. io:format(
  113. "exit by timeout~n",
  114. []
  115. ),
  116. % item 567
  117. ok
  118. end
  119. .
  120. future_time(Seconds) ->
  121. % item 295
  122. Now = calendar:local_time(),
  123. NowSec = calendar:datetime_to_gregorian_seconds(Now),
  124. % item 296
  125. ExpSec = NowSec + Seconds,
  126. % item 297
  127. calendar:gregorian_seconds_to_datetime(ExpSec)
  128. .
  129. hello(FirstName, LastName) ->
  130. % item 203
  131. io:format(
  132. "Hello, ~s ~s~n",
  133. [FirstName, LastName]
  134. )
  135. .
  136. hi() ->
  137. % item 304
  138. io:format("hi~n", [])
  139. .
  140. log_on(User, Password) ->
  141. % item 287
  142. case check_password(User, Password) of true ->
  143. % item 290
  144. attach_session(User)
  145. ; false ->
  146. % item 291
  147. access_denied(User)
  148. end
  149. .
  150. make_pass_hash(Salt, Password) ->
  151. % item 229
  152. PassBinary = unicode:characters_to_binary(
  153. Password,
  154. utf8
  155. ),
  156. ToHash = [Salt, PassBinary],
  157. % item 230
  158. crypto:hash(sha512, ToHash)
  159. .
  160. number_to_word(Number) ->
  161. case Number of
  162. 0 ->
  163. % item 321
  164. "zero"
  165. ;
  166. 1 ->
  167. % item 322
  168. "one"
  169. ;
  170. 2 ->
  171. % item 323
  172. "two"
  173. ;
  174. _ ->
  175. % item 324
  176. "many"
  177. end
  178. .
  179. print_result(Result) ->
  180. case Result of
  181. ok ->
  182. % item 338
  183. io:format(
  184. "it's okay~n",
  185. []
  186. )
  187. ;
  188. {error, Message} ->
  189. % item 339
  190. io:format(
  191. "Error: ~s~n",
  192. [Message]
  193. )
  194. ;
  195. _ ->
  196. % item 340
  197. io:format(
  198. "Unknown: ~p~n",
  199. [Result]
  200. )
  201. end
  202. .
  203. quad_equation(A, B, C) ->
  204. % item 275
  205. case A == 0 of true ->
  206. % item 278
  207. {error, not_quad}
  208. ; false ->
  209. % item 262
  210. Dis = B * B - 4 * A * C,
  211. A2 = 2 * A,
  212. % item 268
  213. case Dis > 0 of true ->
  214. % item 279
  215. SqDis = math:sqrt(Dis),
  216. % item 271
  217. X1 = (-B + SqDis) / A2,
  218. % item 272
  219. X2 = (-B - SqDis) / A2,
  220. % item 273
  221. {ok, X1, X2}
  222. ; false ->
  223. % item 263
  224. case Dis == 0 of true ->
  225. % item 266
  226. X1 = -B / A2,
  227. % item 267
  228. X2 = X1,
  229. % item 273
  230. {ok, X1, X2}
  231. ; false ->
  232. % item 274
  233. {error, no_roots}
  234. end
  235. end
  236. end
  237. .
  238. quicksort(List, Comparer) ->
  239. % item 578
  240. case List =:= [] of true ->
  241. % item 379
  242. Result = List
  243. ; false ->
  244. % item 373
  245. [ Pivot | Others ] = List,
  246. % item 386
  247. LessThanPivot = fun(X) ->
  248. Comparer(X, Pivot) < 0
  249. end,
  250. % item 385
  251. {Left, Right} = lists:partition(
  252. LessThanPivot,
  253. Others
  254. ),
  255. % item 378
  256. LeftSorted = quicksort(Left, Comparer),
  257. RightSorted = quicksort(Right, Comparer),
  258. % item 383
  259. Result = LeftSorted ++ [Pivot] ++ RightSorted
  260. end,
  261. % item 384
  262. Result
  263. .
  264. run() ->
  265. % item 419
  266. hi(),
  267. % item 420
  268. hello("Joe", "Black"),
  269. % item 421
  270. io:format(
  271. "~p~n",
  272. [future_time(0)]
  273. ),
  274. % item 422
  275. io:format(
  276. "~p~n",
  277. [future_time(60)]
  278. ),
  279. % item 423
  280. io:format(
  281. "~p~n",
  282. [future_time(3600)]
  283. ),
  284. % item 424
  285. User = create_user(
  286. "Bender",
  287. "Erlang123",
  288. true
  289. ),
  290. % item 425
  291. log_on(User, "111"),
  292. % item 426
  293. log_on(User, "Erlang123"),
  294. % item 427
  295. io:format(
  296. "Solving quadratic: ~p~n",
  297. [quad_equation(1, -8, 12)]
  298. ),
  299. % item 428
  300. io:format(
  301. "Solving quadratic: ~p~n",
  302. [quad_equation(5, 3, 7)]
  303. ),
  304. % item 429
  305. io:format(
  306. "Solving quadratic: ~p~n",
  307. [quad_equation(1, -6, 9)]
  308. ),
  309. % item 430
  310. io:format(
  311. "Number to word: ~p~n",
  312. [number_to_word(0)]
  313. ),
  314. % item 431
  315. io:format(
  316. "Number to word: ~p~n",
  317. [number_to_word(1)]
  318. ),
  319. % item 432
  320. io:format(
  321. "Number to word: ~p~n",
  322. [number_to_word(25)]
  323. ),
  324. % item 433
  325. print_result(ok),
  326. % item 434
  327. print_result({error, "Division by 0"}),
  328. % item 435
  329. print_result(
  330. "I am an unexpected string"
  331. ),
  332. % item 507
  333. io:format(
  334. "~p~n",
  335. [split("one,two,three", $,)]
  336. ),
  337. % item 508
  338. io:format(
  339. "~p~n",
  340. [split(",one,,,two,three,", $,)]
  341. ),
  342. % item 509
  343. io:format(
  344. "~p~n",
  345. [split(",,,,", $,)]
  346. ),
  347. % item 510
  348. io:format(
  349. "~p~n",
  350. [split("", $,)]
  351. ),
  352. % item 440
  353. IntCmp = fun(X1, X2) ->
  354. X1 - X2
  355. end,
  356. % item 438
  357. io:format(
  358. "~p~n",
  359. [quicksort([5, 5, 5, 5], IntCmp)]
  360. ),
  361. % item 441
  362. io:format(
  363. "~p~n",
  364. [quicksort([], IntCmp)]
  365. ),
  366. % item 442
  367. io:format(
  368. "~p~n",
  369. [quicksort([3], IntCmp)]
  370. ),
  371. % item 443
  372. io:format(
  373. "~p~n",
  374. [quicksort([3, 1, 4, 2, 6, 8, 7, 5], IntCmp)]
  375. ),
  376. % item 572
  377. Pid1 = spawn(fun echo/0),
  378. % item 573
  379. Pid1 ! {msg, "First message"},
  380. % item 574
  381. Pid1 ! {msg, "Second message"},
  382. % item 577
  383. Pid1 ! stop,
  384. % item 575
  385. Pid2 = spawn(fun echo_t/0),
  386. % item 576
  387. Pid2 ! {msg, "Good morning"},
  388. % item 436
  389. io:format("All done!")
  390. .
  391. split(List, Separator) ->
  392. % item 457
  393. Acc = {idle, [], []},
  394. % item 458
  395. {_, Output, Buffer} = split_core(
  396. List,
  397. Separator,
  398. Acc
  399. ),
  400. % item 512
  401. case Buffer =:= [] of true ->
  402. % item 515
  403. OutList = Output
  404. ; false ->
  405. % item 487
  406. OutList = add_buffer(Buffer, Output)
  407. end,
  408. % item 464
  409. lists:reverse(OutList)
  410. .
  411. split_core(List, Separator, Acc) ->
  412. % item 511
  413. {State, Output, Buffer} = Acc,
  414. % item 465
  415. case List =:= [] of true ->
  416. % item 468
  417. Acc
  418. ; false ->
  419. % item 476
  420. [Head | Rest] = List,
  421. case State of
  422. idle ->
  423. % item 488
  424. case Head =:= Separator of true ->
  425. % item 499
  426. Acc2 = {idle, Output, Buffer}
  427. ; false ->
  428. % item 491
  429. Buffer2 = [Head],
  430. % item 492
  431. Acc2 = {token, Output, Buffer2}
  432. end,
  433. % item 502
  434. split_core(
  435. Rest,
  436. Separator,
  437. Acc2
  438. )
  439. ;
  440. token ->
  441. % item 495
  442. case Head =:= Separator of true ->
  443. % item 500
  444. Output2 = add_buffer(
  445. Buffer,
  446. Output
  447. ),
  448. % item 501
  449. Acc2 = {idle, Output2, []}
  450. ; false ->
  451. % item 497
  452. Buffer2 = [Head | Buffer],
  453. % item 498
  454. Acc2 = {token, Output, Buffer2}
  455. end,
  456. % item 502
  457. split_core(
  458. Rest,
  459. Separator,
  460. Acc2
  461. )
  462. ;
  463. _ ->
  464. throw("Unexpected switch value")
  465. end
  466. end
  467. .