New answers tagged

2

As per [Wikipedia], "...a guard is a boolean expression that must evaluate to true if the program execution is to continue in the branch in question...". So in your first example code, you have a default value that the function will return, "Markdown". And you have two guards, that will return "Not supported file type" or "Plain text" if those guards are ...


1

An analogy that made me understand the difference between Sync vs Async vs Multi-threaded is that of a cook in the kitchen. Imagine you are making pasta. You have three steps: Boil and drain the pasta Prepare sauce Combine pasta and sauce Synchronous method. In synchronous scenario there is only one person (thread) doing all the work in sequence. First ...


2

Concrete Examples I would like to add some real-world examples, and connect them to the software engineering world. First, consider something that I hope matches your intuitive definition of "synchronous": the flashing of fireflies, under some circumstances. Second, consider the 4x100 women's Olympic relay race. Third, consider that old trope from ...


12

Asynchronous: not existing or occurring at the same time. Synchronous: existing or occurring at the same time. The async attribute means that the script will be executed as soon as it is downloaded even if the html is still parsing, which means both processes exist at the same time to me. This is indeed confusing! Consider instead the meanings of ...


0

I think the key of your confusion can be summed up by: The async attribute means that the script will be executed as soon as it is downloaded even if the html is still parsing The thing to realise is that this sentence does not make sense because it describes an impossible situation. If the HTML is still parsing then the script download process would not ...


2

Imagine two satellites orbiting Earth. Satellite A has a period of rotation around Earth such that for every full rotation of the planet, the satellite has gone around Earth more or less than one time. Satellite B has a period of rotation around Earth such that for every full rotation of the planet, the satellite has gone around Earth exactly one time. ...


0

"synchronous" means that two events occur at the same time - but which events? When we say "synchronous execution", we mean that the caller and callee are executing (i.e. on stack) at the same time. That's probably the meaning you are after. When we say "synchronous logic gate", we mean that the logic gate is synchronized with the cpu clock. When we say ...


1

One way to think about it are SIMD instructions, like AVX. Here are some examples of how they are used. Synchroneous SIMD instructions allow you to do multiple calculations at precisely the same time, in the same thread, by operating a Single Instruction on Multiple Data. While asynchroneous multithreading allows you to do multiple calculations at "...


6

I'm an electrical engineer, and we dealt with synchronous vs asynchronous in logic circuits (logic gates). Let's say you have an AND-gate (or any gate), which has two inputs, and an output. If it is asynchronous, it will update its output the moment any of the inputs change in such a way that the output changes. This is how your example worked - the ...


48

I would like to give you an answer which is directly related to those definitions you found. When one task T1 starts a second task T2, it can happen in the following manner: Synchronous: existing or occurring at the same time. So T2 is guaranteed to be started and executed inside the time slice of T1. T1 "waits" for the ending of T2 and can continue ...


0

A good question, and terms that are often used in different ways that lead to confusion. My answer is that these terms are relative — and what they are relative to is the main program that is executing (or sometimes to a thread). These terms specify something about the internal operation & timing of a program, as to whether messages are sent or ...


21

I find that the best way to understand it is the following: Synchronous: We know when it will happen (it happens when this other code ends). Asynchronous: We do not know when it will happen. Note: while we can schedule code to be executed a given clock time, in practice we do not know when it will happen, because it can be delayed – even ignoring messing ...


0

You can implement memoization without side-effects using the state monad. [State monad] is basically a function S => (S, A), where S is the type that represents your state and A is the result the function produces - Cats State. In your case the state would be the memoized value or nothing (i.e. Haskell Maybe or Scala Option[A]). If the memoized value is ...


2

Usually, a function that returns a list is not pure at all because it requires allocation of storage and can thereby fail (e.g. by throwing an exception, which is non-pure). A language that has value types and can represent a list as a bounded-size value type may not have this issue. For this reason, your example is probably not pure. In general, if the ...


7

Yes, memoized pure functions are commonly referred to as pure. This is especially common in languages like Haskell, in which memoized, lazily-evaluated, immutable results are a built-in feature. There is one important caveat: the memoizing function must be thread-safe, or else you might get a race condition when two threads both try to call it. One ...


3

It depends on how you do it. Usually people want to memoize by mutating some sort of cache dictionary. This has all the problems associated with impure mutation, such as having to worry about concurrency, worrying about the cache growing too large, etc. However, you can memoize without impure memory mutation. One example is in this answer, where I track ...


41

Yes. The memoized version of a pure function is also a pure function. All that function purity cares about is the effect that input parameters on the return value of the function (passing the same input should always produce the same output) and any side effects relevant to global states (e.g. text to the terminal or UI or network). Computation time and ...


19

Wikipedia defines a "Pure Function" as a function that has the following properties: Its return value is the same for the same arguments (no variation with local static variables, non-local variables, mutable reference arguments or input streams from I/O devices). Its evaluation has no side effects (no mutation of local static variables, non-local variables,...


Top 50 recent answers are included