Getting a bit stuck here on the idea...
I'm trying to create a flash card game / study material program where users are able to load lists of questions/answers. I want to organize the lists in a folder-like system. I imagine folders as arrays and the lists of questions and answers as text files.
My problem is that I know that with a single array, I can dynamically create an infinite amount of arrays... but how do I write data to the last array in the line? The idea is that the USER creates their own organization (dynamic, for different classes, subjects, etc)
I thought about something like myArray[i][j][k][l] but I don't know how to make it dynamic, such as if it was like myArray[3][4] has no subclasses or child arrays, or if [l] had 20 more subclasses/child arrays.
I was thinking about writing a function that has a loop that calls itself if there are children, but, I don't know how to make said unique for [i], [j], [k], etc.
Sorry if the information is a bit underwhelming, but I'm not sure how to express any more information.
Actionscript is object oriented and I will not try to create an array if it has to be dynamic. I will create an object called 'Questions' and will have the question text, text file location or whatever you want to maintain. I will create an ArrayCollection attribute called children with in the 'Question' class, which in turn is a list of Question objects. This way I can maintain hierarchy and add and remove children without much hassle.
Related
I'm currently working on a flutter application. I have a file with a big widget tree. In order to easier understand, read and maintain I wanted to "crop" the widget tree.
First what I did was to create multiple functions, which represented a bigger part of the tree such as _createFancyImage() or _createFancyContainer. After some research, I found out that such a design has some downsides (see: https://github.com/flutter/flutter/issues/19269). So then I decided to create StatelessWidgets instead. Because of the huge size of the widget tree, I broke it down to 3 logical StatelessWidgets. Now I can use FancyImage() or FancyContainer() which represent each a standalone widget.
As a beginner, I'm not sure whether I should keep those StatelessWidgetclasses within the same file. Alternatively, I could create independent files. One thing to clarify: I'm not using those fancy widgets somewhere else. Those are unique to this one big widget tree otherwise I could have outsourced them into a new folder such as "common_widgets" or "components".
Unfortunately, I couldn't find something within the Dart and Flutter Repo style guides nor on the internet.
I appreciate every suggestion.
You can add as much classes as you need in a single file. It depends on the developer's mindset.
But, let say if one of your class can be reused by other classes or packages then you should add it to another file for better separation.
I can favour you one approach is that your each file should have maximum one Stateful widget and as many Stateless widgets as you want for that corresponding widget will be a better scenario.
Still in some cases if you feel that more than enough stateless widgets has been added in a single file you should separate it in another file based on your choice.
I prefer to keep one public widget which having the same name as the filename and remaining private widgets.
comming to your ques is How many widgets in a single file?
Its actually depend there is no such rule to restrict the limit of file. Different authors having different preference. I prefer try to keep 5-6 classes(widgets) and
each one having 5-6 functions.
Try to make a file single responsible i.e(5-6 classes together responsible for single functionality). Don't make god class which having unrelated concerns together later it will pains(haha)
If it's a common widget keep them separate to respect DRY principle(Don't repeat yourself)
If the widget is further divided into 3-4 widgets or it children widget change depend upon rest response keep seprate for good practise
Bonus Tip: try using code folding shortcuts to push a little more
I have a (actually quite simple) data structure that has a tree-like adjacency. I am trying to find a good way to represent data for a film-industry based web-app which needs to store data about film projects. The data consists of:
project -> scene -> shot -> version - each adjacent to the previous in a "one-to-many" fashion.
Right now I am thinking about a simple adjacency list, but I am having trouble believing that it would be sufficiently efficient to quickly retrieve the name of the project, given just the version, as I'd have to cycle through the other tables to get it. The (simplified) layout would be like this:
simple adjacency layout
I was thinking about - instead of referencing only the direct parent - referencing all higher level parents (like this), knowing that the hierarchy has a fixed depth. That way, I could use these shortcuts to get my information with only one query. But is this bad data modeling? Are there any other ways to do it?
It's not good data modelling from the perspective of normalisation. If you realise that you put the wrong scene in for a project, you then have to move it and everything down the hierarchy.
But... does efficiency matter to you? How much data are you talking about? How fast do you need a response? I'd say go with what you've got and if you need it faster, have something that regularly extracts the data to a cache.
Try a method called Modified Preorder Tree Traversal: http://www.sitepoint.com/hierarchical-data-database/
Hi i'm trying to store an array filled with objects so that it doesn't disappear if the app is closed completely.
The Problem:
if I use core data and convert the array to NSData then it works. But the app freezes while it's processing the array.
I've also tried the transformable datatype but i cant't get it to work.
And I can't use NSUserdefaults either because it doesn't support images.
Does anyone have an idea how i might solve this.
i'm quite a newbie to programming so this might be an entirely wrong approach.
First save the images in individual files with unique file names in an image directory in the Documents directory. Put the unique file names in the array, not the images.
Then depending on your needs either save the individual per image information in Core Data if quick random access is required. Or save the array in a file.
For 1500 strings of ~100 characters each saving in a single file is probably fine, I would start there and only move up to Core Data if there is a performance problem Core Data would resolve it.
As Ken Beck says: "Do the simplest thing that could possibly work." I don't believe that having 750 images in the array would really work if they were of any substantial size.
Do not use NSUserDefaults.
Core Data is overkill for simply saving an array to disk.
What kind of objects are you saving?
If you already have logic to convert the array to NSData why not just save the file to either your documents directory or caches directory (caches if it can be recreated if purged, documents if it is unique and contains user state info.)
Edit:
zaph raises some good points in his question. How big is this array (number of elements and total data size.) Is it reasonable to load it all into memory?
If you are looking for a random-access way to load one element at a time, then a database might be reasonable.
The specific solution depends on the particulars of your problem, so we need more info.
This is something I have been struggling about for some time now. The thing is: I have many (20 or so) static arrays of values. I say static because that is how I'm actually storing them, as static arrays inside some domains. For example, if I have a list of known websites, I do:
class Website {
...
static websites = ["web1", "web2" ...]
}
But I do this just while developing, because I can easily change the arrays if needed, but what I'm going to do when the application is ready for deployment? In my project it is very probable that, at some point, these arrays of values change. I've been researching on that matter, one can store application properties inside an external .properties file, but it will be impossible to store an array, even futile, because if some array gets an additional value, the application can't recognize it until the name of the new property is added where needed.
Another approach is to store this information in the database, but for some reason it seems like a waste to add 20 or more tables that will have just two rows, an id and a name.
And the last option, as far as I know, would be an XML, but I'm not very experienced with those. It seems groovy has a way of creating and reading XML files relatively easy, but I don't know how difficult would be to modify an XML whose layout is predefined in the application.
Needless to say that storing them in the config.groovy is not an option since any change will require to recompile.
I haven't come across some "standard" (maybe a best practice?) way of dealing with these.
So the questions is: Where to store these arrays?
Use Enum for a fixed set of attributes. Do this, if you rely at some places in your code on some concrete values.
If you do not rely on the attributes within your code (which you obviously don't), use a String-type. In this case, if you need to provide a select box, just do a distinct-query on this property in your database.
Default: Use a domain class per dynamic set of attributes. Another table is OK.
For something as simple as arrays you should use groovy own type of property files. They allow you too define properties as proper groovy variables (not just strings) and obviously loading them would be done dinamically in a simple way by using ConfigSlurper. For an example on how to use this kind of file you can have a look at the following ConfigSlurper:
For defining properties:
my.property.array=[1,2,3,4]
For loading property files:
def config = new ConfigSlurper().parse(new File('myconfig.groovy').toURL())
assert [1,2,3,4] == config.my.property.array
Tip: When you want to access the property files you want to do it in a way that can work for any environment. To make paths environment-independent use the following path as the root path:
import org.codehaus.groovy.grails.commons.ApplicationHolder
def ctx = ApplicationHolder.application.mainContext.servletContext
def rootPath = ctx.contextPath
The application I'm building is a tennis draw, which has a list of matches organized by round. I've also given the matches a position number as a way to order them and manage matches that are at the top of the draw. In a 32 player draw there are 16 matches in the first round, with match positions ordered from 1 - 16.
The draw is laid out using html and isn't just a series of table rows, it has some complexity to it. So in order to display match players/scores in the view I need to place data in places that are out of sequence from a typical display using a series of rows.
How can I display 16 matches of data without using a foreach loop? Is it best for each variable in the controller be for a single record?
I had thought that in the controller I could have 16 variables, but I'd prefer to learn better dry approaches to programming as I learn php and cakephp.
Much appreciated.
Just display the array elements.
<?php echo $matches['Match'][0]['Player1']?>
<?php echo $matches['Match'][12]['Score']?>
If you want to have a good look at the data array, to figure out which bits are where, you can use pr().
<?php pr($matches)?>
You can just echo out a specific part of an array, they are no exclusivly tied to foreach loops. This is the approach that I would take. Although you need to be aware that obviously as your data changes between rounds you'll either need to update or create a new view for each round.
Don't give up on foreach just yet.
I each row of data is "fairly complex", perhaps it's the best if you move it to an element.
If you need to display data outside of the typical table, maybe it would be better to extract that data using the awesome Set class, and then doing your calculations and whatnot.
Although, as sibidiba has already mentioned, it would be the best if you did all that in your model, so your views stay clean and clear. All the data you want to display outsite your standard table should already be ready for display, prepared in your model and passed on to your views by your controller. As mentioned, Set class is a powerful tool, see if you can use it.
If you are really unable to iterate through the data in your view (consider also nested foreach loops), your are passing the wrong data to the view!
Refactor your controller to pass data in an iterable form. Containables are really awesome in CakePHP.