Posts

Showing posts from July 29, 2018

How to specialize mapM for IO in Haskell

Image
Clash Royale CLAN TAG #URR8PPP How to specialize mapM for IO in Haskell Say I have a task that represents some computation from k to v where some inputs have to be fetched externally. k v newtype Task k v = Task { run ∷ ∀ f. Monad f ⇒ (k → f v) → f v } For some tasks mapM will be used, e.g. to fetch multiple keys. I want to specialize mapM for some monads. Specifically for the IO monad I want to use Control.Concurrent.Async.mapConcurrently to perform actions concurrently. mapM mapM IO Control.Concurrent.Async.mapConcurrently My first instinct is to introduce a wrapper type newtype AsyncIO a = AsyncIO { io :: IO a } and then introduce instance Monad AsyncIO However this doesn't work because in the current GHC implementation mapM is defined in term of traverse , which is in Traversable . mapM traverse Traversable Is there an elegant solution for this? I don't quite understand what you mean by "for some tasks mapM will be used". Could

Why does JavaScript's `function.call` have to be called explicitly?

Image
Clash Royale CLAN TAG #URR8PPP Why does JavaScript's `function.call` have to be called explicitly? I have a string, " test " . It's a really ugly string, so let's trim it. " test " " test ".trim() returns "test" . Nice! " test ".trim() "test" Now let's try to do it with that string as an argument. String.prototype.trim.call(" test ") also returns "test" . Nice again! String.prototype.trim.call(" test ") "test" Oh, that means I can use String.prototype.trim.call to map an array of ugly strings by their trimmed counterparts, right? String.prototype.trim.call [" test "].map(String.prototype.trim.call) does not return ["test"] . [" test "].map(String.prototype.trim.call) ["test"] It throws TypeError: undefined is not a function . TypeError: undefined is not a function Why is this not allowed? .map

Infinite Loop when reading specified number of bytes from binary file

Image
Clash Royale CLAN TAG #URR8PPP Infinite Loop when reading specified number of bytes from binary file I have a binary file that starts something like this (in hex): FFFF7FFF FFEFFFFF FFFFFFFF 0000454C 0000AA0E 2D2D2D2D 00002892 425A6839 31415926 5359F7B8 FA590000 A4FFFFFF FFFFFFFF FFFFFFFF FFFFFDFF EBFFFFFF FFFF7FFF FFEFFFFF FFFFFFFF FFFFFFE0 00 I'm trying to read in the header with C++. I've got code that looks like this: #include <iostream> #include <fstream> #include <vector> #include <string> #include <cstring> using namespace std; typedef struct { char tp[9]; char xt[3]; uint32_t jdt; uint32_t jtm; char nm[4]; } Header_1; int main(){ Header_1 test; ifstream infile("./testbin", ios::in | ios::binary); vector<unsigned char> head_bytes(24, 0); infile.read((char*)&test, head_bytes.size()); vector<unsigned char> comp_record(12, 0); infile.read((char*)&comp_record, comp_record.

chrome html5 video stops autoplay after a second or two

Image
Clash Royale CLAN TAG #URR8PPP chrome html5 video stops autoplay after a second or two I've got an autoplaying video which works well on all browsers, except Chrome - which has apparently disabled autoplay video. I've tried some work arounds, but now it just plays for 1-2 seconds and stops entirely. Here's my code: <video id="introduction-video" preload="auto" playsinline autoplay muted loop volume="0" poster="/images/videos/video-background.png" width="100%" height="100%"> <source src="/images/videos/Tasman10seconds.mp4" type="video/mp4"> <source src="/images/videos/Tasman10seconds.webm" type="video/webm"> Sorry, your browser does not support HTML5 video. </video> <script>$(window).on('load',function(){$('#introduction-video').get(0).play();});</script> And you can check i

Inherit instance values from a base class

Image
Clash Royale CLAN TAG #URR8PPP Inherit instance values from a base class Suppose you have a class relation like this: class banana : fruit { } If there are some public global values already assigned to fruit, is there a way to have a new banana class inherit these existing values? For example: class fruit { public int price; public string origins; } class banana : fruit { public string peelDensity; fruit (peelDensity p, int pr, string o) { peelDensity = p; price = pr; o = origins; } } Say an existing fruit instance already has its price , origins and etc assigned. Suppose that is actually common fruit data that applies to a particular banana. How would the banana inherit these values? price origins Is there a better way to do this than needing to supply all the existing values in the constructor? banana b = new banana(peel, price, origins); It can't inherit that. You will need to copy them over (factory met

Java Prime Number Method

Image
Clash Royale CLAN TAG #URR8PPP Java Prime Number Method The question is exactly as follows: Write a method that determines whether a number is prime. Then use this method to write an application that determines and displays all the prime numbers less than 10,000. I have already written a program that finds all prime numbers up to 10,000, but then found a simpler and more efficient one on StackOverflow, and that is this: package prime; import java.util.Scanner; public class Prime { public static void main(String args) { for(int i = 1; i <= 10000; i++) { int factors = 0; int j = 1; while(j <= i) { if(i % j == 0) { factors++; } j++; } if (factors == 2) { System.out.println(i); } } } } Since I am very new to Java and am especially not good at methods, this

How to show number keyboard for a text input field in mobile application

Image
Clash Royale CLAN TAG #URR8PPP How to show number keyboard for a text input field in mobile application I am using Cordova + react js to build an Android app and I have a requirement to render a currency number on a input field. When this input field get focus, it shows a number keyboard. After users type in some numbers, it will format the number as a text string. For example, if users type 394,333.2930 , it will render it as $394,333.293 . There is no problem for me to format the string. The problem is how can I show a number keyboard for a text input field. I know it works for a input with number type but it doesn't allow users type , or currencty sign. Also with number input field, users can type mutiple dot . which I don't want this happen. So how can I show a number only keyboard for a text input field in Cordova application? I am not sure whether this is a cordova specific problem or a general web application problem. Cordova react js input field 394,333.2930 $394

The height of the HTML tag is set differently than my intention

Image
Clash Royale CLAN TAG #URR8PPP The height of the HTML tag is set differently than my intention my structure is table ㄴ tbody ㄴ tr ㄴ td height: 25px ㄴ div height:24px ㄴ div height: 100% => why 30px ???? Why do I get a 30px when I set the height of the last div to 100%? There isn't enough information here to answer your question. Perhaps you could include a code snippet demonstrating the problem you're running into? – Ollin Boer Bohan 8 mins ago Please post a Minimal, Complete, and Verifiable example. – csmckelvey 7 mins ago By clicking "Post Your Answer", you acknowledge

How can I control the column space according number of elements with CSS grid

Image
Clash Royale CLAN TAG #URR8PPP How can I control the column space according number of elements with CSS grid For example, I'd like to use a class which has at the most 3 columns, but If I had just 2 elements, this row gonna align this elements to the center Is it possible with css grid? as in the example below enter image description here Use flexbox, not grid. The picture you linked is not in a grid layout. – jhpratt 1 min ago By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Custom view has setOnTouchListener called on it but does not override performClick (class extends Activity NOT View)

Image
Clash Royale CLAN TAG #URR8PPP Custom view has setOnTouchListener called on it but does not override performClick (class extends Activity NOT View) I have created an Android Activity implementing a bit of OpenCV. What it does is to simply create a Custom Camera View, and when the screen is touched, the photo will be saved. My only problem is that the code mOpenCvCameraView.setOnTouchListener(MainActivity.this); inside the BaseLoaderCallback contains a warning. mOpenCvCameraView.setOnTouchListener(MainActivity.this); mOpenCvCameraView.setOnTouchListener(MainActivity.this); warns about mOpenCvCameraView.setOnTouchListener(MainActivity.this); Custom view com/example/zcameratestv2/Version2CameraView has setOnTouchListener called on it but does not override performClick Unlike other questions, my class extends an Activity not View, so when i try to override the function private boolean performClick() { ...super.performClick(); } it is not recognized. Here are my classes private boolean p

Connect of android app is timeout with localhost

Image
Clash Royale CLAN TAG #URR8PPP Connect of android app is timeout with localhost I have a localhost and when I try to get data from it from my android app it give me this error: (This site can't be reached 192.168.1.105 took too long to respond ERR_CONNECTION_TIMED_OUT ) can anyone help me ??? By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

How to populate data on nav-tab in partial View in MVC?

Image
Clash Royale CLAN TAG #URR8PPP How to populate data on nav-tab in partial View in MVC? I have partial View where I have 2 nav-tab. On Each tab, I want to populate data based on Country. Currently, I am getting following error: Insufficient stack to continue executing the program safely. This can happen from having too many functions on the call stack or function on the stack using too much stack space. Controller public ActionResult Index() { ClsHome model = new ClsHome(); return View(model); } public PartialViewResult EmployeeBasedCountry(int CountryId) { ClsHome clshome=new ClsHome(); clshome.Country = CountryId; clshome.countries = CountryFilter(CountryId); return PartialView("~/Views/Home/_pEmp.cshtml", clshome); } Index View @model EmpWebsite.Models.Home.ClsHome <div class="col-md-5"> @Html.Partial("_pEmp&q

How to adjust this script to use Glob instead of 1 file

Image
Clash Royale CLAN TAG #URR8PPP How to adjust this script to use Glob instead of 1 file I have created this script based on reading other posts on StackOverflow. The script creates a thumbnail from the original image and adds it to a folder. The source images are located on my local server so Glob would work however I am not sure how to adjust this script so that it will run this function on all files in a folder (Glob). I realize it may be memory intensive but I only need to do it a few times on several folders and I'll be done with it. Just in case you are questioning the reason I have included $height2, it is a little hack I came up with where we create the thumbnail while maintaining the aspect ratio then only save the top 250px so that the thumb is 250 x 250 but not distorted (stretched). It works great. I just need to adjust to do a batch of all images in the source folder. Any help would be great. Please keep in mind that I am a front end developer (html and css) and not gre

According to the button 1 image is shown

Image
Clash Royale CLAN TAG #URR8PPP According to the button 1 image is shown Well, as the title says I want an image to be shown, the issue is that I have several buttons and each one has to show a different image, I have a function that when clicked shows the image that corresponds to each button, the theme is that I need the previous image to be hidden and I also need some advice or a guide to do a doubleclick function, keep the id and it is shown in a text here I leave html, css and the small function that I did .. clearly everything can be improved but I'm I'm using javascript, but I also believe that there is a way by jquery html <!DOCTYPE html> / .general {display: none;} / <body> <div class="full-screen"> <div class="general"> <div class="contain1"> <button class="boxsmall" id="bate"> <div clas

/bin/sh Need to identify file content type

Image
Clash Royale CLAN TAG #URR8PPP /bin/sh Need to identify file content type I'm working on busybox and have only /bin/sh available. I would like to understand if the file I'm processing with my script are to be treated as ASCII (just read and do what I need to do) or gzip (so unzip first then do what I need to do). The "file" command here would be perfect, but unfortunately it's just not available, hence I don't know what procedure to call as the input file I'm processing can be either format. I'm wondering if there's a simple workaround I'm missing here to find this out... What do you consider "non-ASCII"? Do you just need to test if your file has no characters with values outside 0-127? – Charles Duffy 13 mins ago You could check for the same magic number file l

Disable new attributes values for WP All import Woocommerce

Image
Clash Royale CLAN TAG #URR8PPP Disable new attributes values for WP All import Woocommerce Is it possible to create some function to disable WP All import to create new attribute values? I want to create a function that will update product attribute values only if there is already in Woocommerce. I want to do this because it is very hard to map all attribute values when it comes to 5k products and more. By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.