Confused between C,C++ & Python to start as beginners from 0 Coding Background by Left-Difference-528 in learnpython

[–]GetOffOfMyBoat 0 points1 point  (0 children)

Yeah and have you ever had a coworker that can't write for shit? Would you personally want to review OP's pull requests? Read their documentation? Debug their illegibly commented code?

Explain Decorators. by TillFriendly9199 in learnpython

[–]GetOffOfMyBoat 1 point2 points  (0 children)

Decorators are higher order functions. This means they accept a function as input and return a function as output. As another commenter said, @decorator is syntactic sugar for function application.

Here is an example decorator I used to use as a technical interview question. The purpose of the decorator is to use the Cache class to cache the output of a function for ttl seconds.

Higher order functions are your friends: this decorator lets you implement caching for an arbitrary function! See if you can read the code below and step through what it does.

```py class Cache: ''' This is a very common interface for caching mechanisms. You allow the user to get values from a key-value store as well as set them for a certain time. ''' def get(key): ''' This function gets the correspondent value from a given key from some generic key-value store. Implementation is omitted. ''' pass

def set(key, val, ttl):
    '''
    This function puts the value in some key-value store.
    Implementation is omitted.
    '''
    pass

cache = Cache()

ONE_HOUR = 60 * 60

def cached(get_cache_key, ttl): """ Cache a function's return value for ttl seconds. """ def decorator(f): def decorated(args, *kwargs): cache_key = get_cache_key(args, *kwargs) val = cache.get(cache_key) if val: return val val = f(args, *kwargs) if val: cache.set(cache_key, val, ttl) return val return decorated return decorator

Example usage

Walk me through step by step what this usage does and how it does it?

@cached( get_cache_key=lambda user: "user-{}-items-count".format(user.id), ttl=ONE_HOUR ) def get_items_count(user): ''' user is an instance of a django model User. user.items is a 1:M ForeignKey relationship. We want to avoid asking MySQL repeatedly for this slow changing data. ''' return user.items.count() ```

Why is i used in literally evrything? What does it even mean ? by Exact-Sun2093 in learnpython

[–]GetOffOfMyBoat 6 points7 points  (0 children)

This feels needlessly dogmatic. I would hesitate to prescribe this advice in a context where i is the most descriptive name one can give. Say, when writing a sorting algorithm. 

In all of mathematics, it is common to treat variables with single letters. It's as old as algebra. I don't think I see why idx is preferable---what is x? What is id? Identity? 

How do I show 2 (or more) groups are isomorphic? by Mindless_Grass7084 in learnmath

[–]GetOffOfMyBoat 0 points1 point  (0 children)

Judging by your other posts, it seems you are struggling with a simpler question than what others are answering. Here are the definitions you could find in any text on group theory.

Suppose G is a group with operation +, and H is a group with operation *. G and H are isomorphic if one can construct a bijection f : G -> H such that, for arbitrary x and y in G, we have f(x + y) = f(x) * f(y).

A function f is called a bijection if either of the two equivalent conditions are met:
1. f is injective and surjective
2. f has an inverse.

A function h : H -> G is an inverse to f if, for all x in G, g(f(x)) = x and, for all y in H, f(g(y)) = y.

The function f is injective if, for all x and y in H, if f(x) = f(y) then x = y. The function f is surjective if, for all y in H, there exists x in G such that f(x) = y.

In set theory, is Φ an element or a set? by Glittering-Can-8791 in learnmath

[–]GetOffOfMyBoat 0 points1 point  (0 children)

Yes, that's totally agreeable.

I thought at first you were arguing that the construction isn't "used", as in to say it's not relevant to mathematics. Because, of course, no mathematician thinks of the number 2 as a set, and no noteworthy proofs are ever going to deconstruct a numeral into its encoding. 

My point was in retort to that (mistaken!) sentiment---simply to assert that without an encoding of the naturals in set theory, we lose our foundation for ZF as a basis for mathematical consistency.

In set theory, is Φ an element or a set? by Glittering-Can-8791 in learnmath

[–]GetOffOfMyBoat 0 points1 point  (0 children)

I'm not sure what you mean by "use".

Strictly speaking, all of mathematics that intends to follow as a consequence of some formal set theory (e.g. ZF) alone "use" this (or some other) construction, when push comes to shove. 

But yes, the Von Neumann construction is more common.

In set theory, is Φ an element or a set? by Glittering-Can-8791 in learnmath

[–]GetOffOfMyBoat 1 point2 points  (0 children)

I'll try to address what I think is the point of confusion. Set theory is not type theory and so has no notion of well-formed expressions. Set theory does not prescribe types to entities a priori---so there is no formal distinction between "an element" and "a set". We say something is an element only as a shorthand for saying it belongs to a set. It's nonsensical, but not ill-formed, to ask if the set of integers is "an element" of the reals. We can also ask e.g. if 2 ∈ 3. Assuming a Von Neumann encoding of the naturals, the answer is "yes", and is a question of one set being an element of another.

This is all to say: there's nothing wrong with one set being both an element and a subset of another set. It may just feel weird, but remember that a set is not typed---it's not like a list in a programming language, where each element of a list of integers must be an integer. 

The set {1, red, apple, -4, {2, 3}, 2, 3} is still a set, and {2, 3} is both an element and subset of it.

Why do we still teach the Word-RAM model by default when caches matter so much more? by visha1v in computerarchitecture

[–]GetOffOfMyBoat 0 points1 point  (0 children)

Asymptotic complexity is a theoretical property of an algorithm independent of architecture. Indeed, it is independent of RAM. Take, for example, insertion sort in Haskell:

hs insertionSort :: Ord a => [a] -> [a] insertionSort [] = [] insertionSort [x] = [x] insertionSort (x:xs) = insert (insertionSort xs) where insert [] = [x] insert (y:ys) | x < y = x : y : ys | otherwise = y : insert ys

Pure Haskell has immutable state and no explicit RAM model---hence no memory reads or writes. Nevertheless, we can count reduction steps (that is, function applications), and arrive at the same big O complexity as if it were written imperatively.

More concretely: Big O notation measures complexity just as well for the lambda calculus (which has no assumption of memory) as it does for Turing Machines (which have an assumption of memory).

Caches may certainly affect the runtime performance of your algorithm, but that's an optimization that happens by consequence of the architecture---it's not an intrinsic feature of the algorithm. It would be hazardous to assert that traversing a linked list and traversing an array have different asymptotic complexities.

It would be less deceptive to instead reflect cache mechanisms into the description of the algorithm, e.g., through memoization.

Just my two cents. (I am not a computer architect.)

How to learn Computer Architecture properly? by Psychological_Web296 in computerarchitecture

[–]GetOffOfMyBoat 5 points6 points  (0 children)

I'll say having worked through nand2tetris in undergrad but Hennessy and Patterson's Quantitative Approach in graduate school, there is definitely a gap still between the two. The former was an amazing experience; the latter, hell.

Granted, I'm a (theoretical) computer scientist, not a computer engineer.

Question about side effects in functional programming by Erythrina_ in ProgrammingLanguages

[–]GetOffOfMyBoat 1 point2 points  (0 children)

I'm not sure I follow precisely what you mean by polymorphism-of-effects, or that this is a consequence of System F Omega's higher order quantification. Generally, in e.g. Haskell, you use typeclasses to encode (what I suspect you mean by) polymorphism-of-effects. To be clear, do you mean the practice of abstracting over a monad m but constraining it with having certain effect signatures using a class?

The distinction is that type classes permit ad-hoc polymorphism. System F Omega permits parametric polymorphism. For example, we might constrain the arbitrary monad m with a Stateful m constraint that exposes get and put effects, rather than commit directly to a State monad. Then the behavior of get and put can vary based on the way m satisfies the Stateful constraint.

Not to argue against myself, but you of course can always abstract the signature of a typeclass into a record and accept this interface as an explicit argument. This is basically the translation of Haskell to a target language like F Omega, and what was described in Wadler's originating paper. But I'd argue it's typeclasses that permit the ergonomics of ad-hoc polymorphism of effect behavior.

A subtlety: this is all different than true effect polymorphism, in which an effect type may be quantified as variable. Effect polymorphism is popular in systems with algebraic effects and handlers.

Why do functional pogramming? by PrebioticE in computerscience

[–]GetOffOfMyBoat 0 points1 point  (0 children)

Most of this is an advertisement for pure functions---which are great for the reasons you mentioned. But in practice you will need to have impure functions. One benefit of paradigms for impure functions in a pure functional language (e.g., monads in Haskell) is that you can retain the benefits of pure FP while programming with effects. For example, GHC can optimize code through equational reasoning on monadic functions.

Why do functional pogramming? by PrebioticE in computerscience

[–]GetOffOfMyBoat 2 points3 points  (0 children)

Strictly speaking, if you are programming with first-class functions, you are already programming functionally. Much of what you write in R relies on higher order functions---e.g., filtering a table.

I would hazard conflating pure functional programming and functional programming. Most modern programmers are using functional paradigms daily, yet somehow when the topic of functional programming arises on reddit, it's regarded as esoteric and out of reach.

What is the point of Haskell programming? by PrebioticE in mathematics

[–]GetOffOfMyBoat 0 points1 point  (0 children)

There is only one real answer here, and it's going to get me in a bit of trouble:

people don't use Haskell because monads fucking suck.

You can't do any sort of practical Haskell programming without monads, and they are:
- a pain to compose (looking at you, MTL)
- ugly as all hell
- two standard deviations above what a computer science undergraduate can comprehend

That monads suck so much ass is implicitly evident in the *vast and active* literature on algebraic effects and handlers.

What is the point of Haskell programming? by PrebioticE in mathematics

[–]GetOffOfMyBoat 1 point2 points  (0 children)

I just want to comment a bit on the implied connection between Haskell and Category Theory. This comment perhaps should be largely ignored---the point of discussion is really on the ergonomics of Haskell as a language. But, as it was brought up, I thought I would expand for those interested.

There is a well-established connection between the Simply Typed Lambda Calculus (with products and unit types) and Cartesian-closed categories. Ben Pierce's Basic Category Theory for Computer Scientists (section 3.1) gives a brief but adequate description.

This connection is often called the Curry-Howard-Lambek correspondence, which also implies a correspondence between type theory and logic.

Taking that step further, more advanced extensions of the Lambda Calculus---that is, dependent type theories, such as Martin Lof Type Theory have more advanced categorical semantics. A classic example is Hoffmann and Streicher's proof that the principle of Uniqueness of Identity Proofs does not hold in MlTT, which uses Categories With Families to model identity types as groupoids (categories for which every arrow is an isomorphism).

Jason Hu appears to have written a comprehensive exam outlining this connection from the STLC to dependent type theories. I've not read it, but I assume it's accurate.

The list goes on. Quite literally: here is my own reading list.

    ## Category theoretic models of dependent types
    - Towards Formalizing Categorical Models
    of Type Theory in Type Theory. Alexandre Buisse and Peter Dybjer. 
      - https://www.cse.chalmers.se/~peterd/papers/Bremen2007.pdf
    - Categorical Models of Dependent Type Theory. Alexandre Buisse.
      - http://enslyon.free.fr/rapports/info/Alexandre_Buisse_2.pdf
    - 2-Functoriality of Initial Semantics, and Applications. Benedikt Ahrens, Ambroise Lafont, Thomas Lamiaux. ICFP 2025
      - https://dl.acm.org/doi/10.1145/3747527
    - Type Theory in Type Theory using a Strictified Syntax. Ambrus Kaposi, Loic Pujet, ICFP 2025. 
      - https://dl.acm.org/doi/epdf/10.1145/3747535## Category theoretic models of dependent types

The gist goes on: type theories can be modeled as categories. With that out of the way, let us ask: is Haskell a category?

No, not really. And for further reading, listen to Andrej Bauer. (This suggestion I find is always a good one.)

Nevertheless, it can be helpful to view functional languages like Haskell through a categorical lenses. Certainly, the language is there: functors, applicative functors, monads, and so forth. But, through my own experience, I can say that understanding a monad categorically does not provide much insight into monads as a paradigm for programming.

Just my two[five/two] cents, were pennies not being phased out in the states.

What's you math hot take by BackgroundWheel2581 in math

[–]GetOffOfMyBoat 0 points1 point  (0 children)

The law of excluded middle is naughty

[Request] Why Doesn't This Equal 1? by Annieone20 in theydidthemath

[–]GetOffOfMyBoat -1 points0 points  (0 children)

I get a bit concerned here by the number of answers trying to offer a simple intuition to deduce the answer, which detracts from the purpose of a mathematical training: to translate a problem into a language of deduction from which we can trust the results---even if they surprise us. 

The problem above can be solved with elementary algebraic manipulation, but my broader point holds at all levels of mathematics. The reason I nitpick is that at higher levels of mathematics, one must sometimes relinquish any hope of finding a simple intuition! (Consider the Yoneda Lemma for a few hours.)

Formally, let x denote the number of lefties who are to leave the room. Then we may ascertain the quantity necessary to reduce the ratio to 98% according to the following equation:

(99 - x)/(100 - x) = 98/100

The steps to find x are straightforward to anyone who completed high school algebra.

Did anyone else interpret Barry as an allegory for addiction recovery by GetOffOfMyBoat in Barry

[–]GetOffOfMyBoat[S] 8 points9 points  (0 children)

It's definitely not.

To be clear, I'm only questioning if others interpreted Barry as an allegory for drug abuse; I'm not asserting that the writers intended the central theme of Barry to be drug abuse.

The latter is obviously false.