A list of puns related to "Functional predicate"
This bug wasn't reported by myself but I think it could use attention and discussion.
This has been around for a while and is quite a frustrating issue. If it were fixed, it would open up so many possibilites for mapmakers and datapack developers.
I've seen a lot of people saying it should be WAI due to how predicates are set up in the source code, but it's still called from the predicate file and the condition isn't flagged as invalid by the output log, so it seems more like an oversight by the devs than something that was intended.
Thoughts?
Edit: link
I'm currently reading about how functional programming concepts can be applied to JavaScript. I wrote up this post showing how to solve a fairly simple challenge using these initially alien concepts. I'd love to see this kind of thing use more widely throughout the JavaScript community.
#[allow(incomplete_features)]
#![feature(generic_const_exprs)]
fn something<T>(val: T)
where
Assert<{ core::mem::size_of::<T>() < 768 }>: IsTrue,
// ^-----------------------------^ any const-expression will do
{
//
}
fn main() {
something([0u8; 0]); // ok
something([0u8; 512]); // ok
something([0u8; 1024]); // compilation error, yass
}
// ---
pub enum Assert<const CHECK: bool> {
//
}
pub trait IsTrue {
//
}
impl IsTrue for Assert<true> {
//
}
Example use cases include trying to write an async executor for a platform that has tight RAM limits π
I am using type predicates to build a validation function that depends on two generic types, the second of which is declared inside the first. How do I define an exportable type for the validation function that users of my library can use? Here is a simple test case:
type Nice = <D>(d: D) => d is D
function s0<S>(s: S) {
function s1() { }
function s2<D>(d: D): d is (D & S) {
Object.assign(d, s)
return true
}
s1.s2 = s2
return s1
}
let i1 = s0({ a: 1 })
// I want:
// let i1: Nice = s0({ a: 1 })
// BUT d1.a will then fail
let d1 = { b: 2 }
if (i1.s2(d1)) {
console.log(d1) // prints { a: 1, b: 2}
console.log(d1.a) // from S
console.log(d1.b) // from D
// console.log(d1.c) // fails as intended
}
What should the type declaration for Nice
be? How do I get "hold" of the S
generic outside of function s0
?
hello I'm looking for a human to tell me at least one difference to this prolog code:
dontknowhowtocallthis(N,N,I,C,C):-
0 =\= N mod I, !.
dontknowhowtocallthis(N,Na,I,Ca,C):-
0 is N mod I,
N1 is N / I,
C1 is Ca+1,
dontknowhowtocallthis(N1,Na,I,C1,C).
dontknowhowtocallthiseither(N,I,R,R):-
I>N, !.
dontknowhowtocallthiseither(N,I,Ra,R):-
I=<N,
I1 is I+1,
(0 is N mod I->
dontknowhowtocallthis(N,N1,I,0,C),
R1 is Ra*((C-1)*3)+4,
dontknowhowtocallthiseither(N1,I1,R1,R)
;
dontknowhowtocallthiseither(N,I1,Ra,R)
).
dontknowhowtocallthiseither(N,R):-
dontknowhowtocallthiseither(N,2,1,R).
c(K, R):-
N is sqrt(K),
N2 is floor(N),
(N =\= N2->
R=0
;
dontknowhowtocallthiseither(N2,R)
).
that will make it behave like the following c(k) js function:
function c(k) {
let n = Math.sqrt(k), res = 1;
if (Math.floor(n) !== n) return 0;
for (let i = 2; i <= n; i++) {
if (n % i !== 0) continue;
let count = 0;
while (n % i === 0) {
n = n / i;
count++;
}
res *= ((count-1) * 3) + 4;
}
return res;
}
Needless to say, I consider myself a prolog newbie and I often get stuck when trying to translate something from an imperative language into prolog.
Something that takes me a couple hours to get a hold on when learning a new imperative language (such as translating that piece of js code) is honestly consuming years for me to adjust to in prolog. I've been practicing for years, and I can't translate that piece of sh*t function with two loops. It's really discouraging. Every time I have to ask a question such as this one on reddit I just feel like I'm never going to get it. This is not for homework or anything of the sort, I just want to learn to use prolog because I think it's such a neat language. But it too often feels like futile effort, only for someone on the internet post a simple, absolutely easy-to-verify solution I may never be able to produce myself in the context of this language.
The fact that it's comparatively a small community makes things even harder because there's barely no place to just read some damn code to try and get it.
Naming stuff is also a huge internal battle because p
... keep reading on reddit β‘Let's say I have a list of pair(Int,Int) v, and I want to first apply a map to it then take out all the elements that have a 1 before them.
v.map{it.first == it.second}.filterIndexed { index, b ->
///And here I have a problem
}
I can't do v[index - 1], since we don't care about v[index - 1], we care about the mapped version. We could do a map first and then do this in another variable but we shouldn't have to compromise on composability to solve a problem this simple. What's an elegant solution?
Hi all,
I published a package including a collection of type predicates and assertion functions. It took a while to code and document because its quite comprehensive. I'd love some stars and especially some input. Here is the link:
I am trying to make a custom link-hint type for org agenda items (e.g., each line that is not a block header in org agenda). To do so I need a predicate function that returns non-nil if the thing at point is an item that could be acted on by `org-agenda-goto`.
I am having troubling finding such a function but I imagine it must exist. Help would be much appreciated!
For example if I have
(define lofuncs (list boolean?, func2 ...))
and I want to grab the boolean? predicate and use it.
(first lofuncs 2) ;; 2 is not a boolean, so returns False.
The above won't work because "first" doesn't know what to do with the 2nd argument.
Is there another way to do this?
It would be nice if there is function that takes a function as an argument and should return #t when the given function is a predicate, #f otherwise. For example:
the structure would be: (predicate? fn)
(predicate? even?) => #t
(predicate? add1) => #f
(predicate? prime?) => #t
(predicate? expt) => #f
The package contains various predicate functions that can be imported in your project.
Import in DrRacket IDE: - Clone the project in the local using `git clone <repo name>` - DrRacket -> File -> Install Package - Under Package Source paste: https://github.com/aryaghan-mutum/racket-predicates - Click Install or Update - Open a file and import the package: (require racket-predicates)
Hi, I am trying to implement a function by the name list? to check if it's argument is a list or not. Essentially it supposed to return either true or false, since it is a predicate procedure. Any ideas?
For ex:
#lang racket
(require rackunit)
(check-true (list? '()))
(check-true (list? '(a b)))
(check-false (list? '(a . b)))
(check-false (list? #f))
(check-false (list? 2))
(check-false (list? 'lisp))
(check-false (list? "lisp"))
Hi everyone, I'm looking for some help concerning display-buffer-alist
.
I want to add a rule for .pdf
files that are created with LaTeX. My first thought was to use a predicate function that, given a .pdf
, checks if there exists a corresponding .tex
in the same folder.
According to the documentation of display-buffer-alist
, it should be possible to specify a predicate function taking two arguments instead of a regexp, so I wrote a small function and added it to display-buffer-alist
:
(defun cst/latex-pdf-p (buffer-name action)
"Determine if there is a corresponding .tex file in the current folder."
(when (string= "pdf" (file-name-extension buffer-name))
(file-exists-p (concat (file-name-base buffer-name) ".tex"))))
(setq display-buffer-alist
`((#'cst/latex-pdf-p . ((display-buffer-reuse-window display-buffer-in-side-window)
(side . right)
(slot . 1)
(window-width . 0.33)
(reusable-frames . nil)))))
It doesn't work however, the rule does not get applied to any .pdf
file. The predicate function alone works when called from a buffer visiting a .pdf
file.
I feel like I misunderstand how the function should be specified, also I don't see why it has to get to arguments instead of just one (the name of the buffer).
Can anyone help me out here? Thanks in advance!
I'm trying to use a member function as a predicate in a call to std::any_of, but somehow I can't get the syntax right. I have the following vector:
std::vector<std::unique_ptr<Foo>> foos;
and a member function
Foo::canHandle(const std::string& command);
then I call
std::any_of(foos.cbegin(), foos.cend(), [this](const std::string& cmd) { return canHandle(cmd); })
which gives me an error like
error C2664: 'bool Foo::someFunction::<lambda\_367e86da87c92c47ac2e3bfff06b1b6b>::operator ()(const std::string &) const': cannot convert argument 1 from 'const std::unique\_ptr<Foo,std::default\_delete<Foo>>' to 'const std::string &'
I'm sure I'm messing things up quite a bit here, but can't seem to get the syntax right... any help appreciated :-)
Why are they called predicate functions? They just seem like regular functions to be called in my opinion.
Hi so I'm a bit confused as to the difference between predicates and functions. I understand they have different functors, but what exactly makes them different? Is it just the use of an operator? Or the ability to prove true/false or smth?
Thanks!
Recently, I've came up with a new method of implementing closures in C. The idiomatic way is passing a closure as a pair of a function pointer and a void*
value as the context. However, this idiom is a bit cumbersome. Firstly, two entities have to maintained instead of one. The other problem is which parameter should the context be bound to. It can be really confusing when the function takes multiple void*
as parameters.
The main obstacle for having nice closures C is binding data to a function pointer. It cannot be done robustly without creating function on fly with a library or compiler extension like "nested functions" in GCC. However, there is a creature that combines a function and data. And this creature is a pointer to a function pointer. The function pointer is data though it points to non-data. It can be used to implement a wide function pointer.
Take a look on the following C89 compliant code:
#include <stdio.h>
typedef int filter_fn(void*, int);
/* prints number from range 1 to `n` that satisfy condition from `filter` */
void print_filtered(int n, filter_fn **filter) {
int i;
for (i = 1; i <= n; ++i)
if ((*filter)(filter, i))
printf("%d ", i);
puts("");
}
struct is_divisible {
int (*closure)(void*,int);
int divisor;
};
int is_divisible(void* closure, int n) {
struct is_divisible *ctx = closure;
return (n % ctx->divisor) == 0;
}
int is_prime(void *closure, int n) {
int d;
(void)closure;
if (n <= 1) return 0;
for (d = 2; d * d <= n; ++d)
if (n % d == 0)
return 0;
return 1;
}
int main() {
struct is_divisible is_divisible_by_3 = { is_divisible, 3 };
static int (*is_prime_closure)(void*,int) = is_prime;
puts("Divisible by 3");
print_filtered(20, &is_divisible_by_3.closure);
puts("Primes");
print_filtered(20, &is_prime_closure);
return 0;
}
It prints the expected output:
Divisible by 3
3 6 9 12 15 18
Primes
2 3 5 7 11 13 17 19
The trick is to place a function pointer as a first member of the struct representing the captured context. This first member can be safely cast to the context. It is is guaranteed to work by the C standard, see https://port70.net/~nsz/c/c11/n1570.html#6.7.2.1p15
>
... keep reading on reddit β‘For my language, Flogram, I've been designing sum types so they can either be a object type or a label, basically an enum. Can have as many labels as possible.
Example:
new tree_node : node | _no_node = _no_node
The above line creates a new variable called tree_node, node is an object, _no_node is a enum and I'm assigning an initial value of _no_node.
But I'm looking at Rust and Haskell and noticing they allow both labels as object type to be mixed. I don't see places where it would be all that useful, it's easy to work around but adding a type to the object that can only be enums if needed, and it's one more concept to add to the language which I'm trying to avoid unless needed, but maybe it is more useful more often than I realize.
Something like this:
new my_car : Subaru>Car | Ford>Car | >Car | No_Car
Where my is an object that either have a label 'Subaru' and be a Car object, or have a 'label' Ford and have a Car Object, or be a Car without a label, or have a label 'No_Car' without
If something doesn't have both a label and object type then if it has > infront of it, it's an Object type, if it doesn't have '>' in front, then it's a label.
This also becomes more complicated when it comes to matching, because without types:
if(tree_node is node){
}
But is I have labels, is doesn't really make sense for labels, like this, has to be:
if(my_car is car){
}
if(my_car labeled Ford){
}
Would love to hear your thoughts. Thanks in advance, love this community!
...but in case you don't:
that pronoun (1)
\ ΛtΝhat
, tΝhΙt
plural those\ ΛtΝhΕz
Definition of that
(Entry 1 of 5) 1a : the person, thing, or idea indicated, mentioned, or understood from the situation that is my father b : the time, action, or event specified after that I went to bed c : the kind or thing specified as follows the purest water is that produced by distillation d : one or a group of the indicated kind that's a catβquick and agile 2a : the one farther away or less immediately under observation or discussion those are maples and these are elms b : the former one 3a βused as a function word after and to indicate emphatic repetition of the idea expressed by a previous word or phrase he was helpful, and that to an unusual degree b βused as a function word immediately before or after a word group consisting of a verbal auxiliary or a form of the verb be preceded by there or a personal pronoun subject to indicate emphatic repetition of the idea expressed by a previous verb or predicate noun or predicate adjective is she capable? She is that
4a : the one : the thing : the kind : something, anything the truth of that which is true the senses are that whereby we experience the world what's that you say b those plural : some persons those who think the time has come
Hi Java people of Reddit:
Does anyone know of any existing libraries with standard useful utility predicates and functions? I'm thinking of utilities like not null / not empty, e.g. being able to write things like:
someListOfStrings.stream().filter(HelpfulPredicatesLibrary::notNullNotEmpty).collect(Collectors.toList());
Thanks!
It seems like with the rise of monadic pattern in other languages, tutorials regarding functor & monad seemed to have improved by a lot. It looks to me that the infamous monad tutorial problem is solved - ppl can learn what is monad / functor without much difficulty compared to learning other patterns. I also tried explaining functor & monad to my mother, who's over 60s now. She have never done programming past COBOL era (lol). However, she said that the concept itself seems quite trivial. (Concurrency was harder to explain) If so, the learning problem with haskell is less with functor/monads, right? To me, the culprit seems to be the error messages. (E.g. Having to learn monad to comprehend IO-related type errors)
P(x,y) is the predicate function: (Domain: all rational numbers).
x+2y=xy
I am curious whether or not the following two propositions are true or false.
βxβy P(x,y)
βxβy P(x,y)
I guess 1) is false. Since this kind of stuff works exactly like a nested for loop right? basically I pick an y for instance y=0, and then I see if the statement P(x,0) is true for all x. it is clearly not for x=1 or x=2, next. I see if it is true for y=1, ie if p(x,1) is true for all x, it is not for instance it isn't for x=0, etc and like that I can continue and since I can never find a y where p(x,y) holds for all x this statement is false.
Is my reasoning correct here?
Now what about the second one here.
I pick an y for instance y=0, does it then exist an x? where p(x,0) is true? yes. but what about y=1? is p(x,0) for 1 x ? I am getting no here. since βx (x+2=x(1)) doesn't really make much sense since I get 0=2. (no matter what I pick x). Is the way I am going about this correct?
Also, another question:
is p β (Β¬q β q) β‘ q
a tautology?
I suppose this is just about checking the truth table for
p β (Β¬q β q) β q
right? or am I misunderstanding?
Hi /r/haskell,
I absolutely love the idea of property-based testing, but I have a hard time coming up with good tests. A lot of the work I do is stringing maps, filters and sorts together. The thing is that I feel that I am not testing my ideas behind the composing of the functions, but rather the prelude functions themselves.
To give an example to guide the discussion, I had to write a program that given a blacklist of ids and a list of sets of ids has to remove the sets that contain one or more of the ids in the blacklist. (The original is written in python, but is reproduced here in Haskell. It's essentially the same).
import qualified Data.Set as Set
blacklist = Set.fromList [2,3]
idset = [Set.fromList[2,5,6], Set.fromList[5,4,6]]
blacklist_filtering = filter (Set.null . Set.intersection blacklist)
main = print $ blacklist_filtering idset
{-- result := fromList[5,4,6] --}
So how would one test this? We could test that when we apply the blacklist filter again on the already filtered list that we should get the same result. However, does this really test my logic or is this more a test for filter? I think this is more a test for filter. One could argue that in a language like python which has side-effects/states that this is still useful. However I know that my function behaves as a function without side effects.
What would be a good test for blacklist_filtering
, both in a functional language and in a non-functional language like python (which has property based testing support via the hypothesis package)? Or is this something you normally wouldn't test?
Furthermore, this question isn't limited to filter. You could do the same for sort, takewhile, etcetera, almost everything that takes a predicate or a key function. To expand, sort also acts idempotent, but you aren't really testing your 'key function' on which you would want to sort if you sort the same list again. (Assuming your using the prelude sort and not a custom one).
Lots of functions in haskell seem to deal with functions of the type
a -> Bool
and there have been several times recently when I have wanted to apply logical operators to two such functions. Something like
pand :: (a -> Bool) -> (a -> Bool) -> a -> Bool
pand f g x = f x && g x
and the equivalent functions for other logical operators like or, xor, nand, nor. It seems like Data.Function.Predicate is the perfect place to toss these functions, but it feels like a pretty barren place to me.
I feel like I must be missing a really obvious way to transform (&&) into the above function. Is there a slick way to accomplish what I'm after without just defining the functions myself?
Why are they called predicate functions? They just seem like regular functions to be called in my opinion.
Please note that this site uses cookies to personalise content and adverts, to provide social media features, and to analyse web traffic. Click here for more information.